Skip to content

Instantly share code, notes, and snippets.

@gorango
Last active September 21, 2016 02:47
Show Gist options
  • Save gorango/3afd8e3680b03ecead3da4136b0b0771 to your computer and use it in GitHub Desktop.
Save gorango/3afd8e3680b03ecead3da4136b0b0771 to your computer and use it in GitHub Desktop.
ng2 infinite scroll directive
import {Directive, ElementRef, EventEmitter, Input, Output} from '@angular/core';
/**
* The infinite scroll directive.
* Providing a number on input offsets the distance from the bottom
*/
@Directive({
selector: '[infiniteScroll]',
host: {
'(scroll)': 'scroll($event)'
}
})
export class InfiniteScroll {
@Output() onScroll = new EventEmitter<any>();
@Input('infiniteScroll') offset: number;
public element: HTMLElement;
constructor(private _element: ElementRef) {
this.element = this._element.nativeElement;
}
scroll() {
let breakpoint = this.element.scrollTop + this.element.clientHeight + this.offset;
if (breakpoint >= this.element.scrollHeight) { this.onScroll.emit(null); }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment