1

I am using the below infinite scroll directive:

https://github.com/orizens/angular2-infinite-scroll

But the thing is when I use this directive on one page, and if I move away from that page the scrolled event is still fired, probably because of below line in the scroller.ts file:

return this.container.addEventListener('scroll', this.handler.bind(this));

This event listener is added but not removed when we move away from the page.

Any clue, how to handle it automatically, when we switch to another page?

1 Answer 1

1

Either you add the event handler declaratively

<div class="container" (scroll)="handler($event)"></div>

or you use

ngOnDestroy() {
  this.container.removeEventListener('scroll', this.handler);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much Günter for your reply. It seems that the ngOnDestroy event isn't being fired, does it work on directives as well? I am using RC1
@NaveedAhmed does your class implement OnDestroy?
Yes, import { Directive, ElementRef, Input, Output, EventEmitter, OnDestroy} from '@angular/core'; export class InfiniteScroll implements OnDestroy { ///other code.... ngOnDestroy() { console.log("ngOnDestroy called"); this.container.removeEventListener('scroll', this.handler); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.