How to get on scroll events?

Natanael picture Natanael · Dec 23, 2016 · Viewed 125.8k times · Source

I need to get scroll events from a div with overflow: scroll in my Angular 2 app.

It seems onscroll event do not works on Angular 2.

How could I achieve that?

Answer

Günter Zöchbauer picture Günter Zöchbauer · Dec 23, 2016
// @HostListener('scroll', ['$event']) // for scroll events of the current element
@HostListener('window:scroll', ['$event']) // for window scroll events
onScroll(event) {
  ...
}

or

<div (scroll)="onScroll($event)"></div>