Skip to content

Instantly share code, notes, and snippets.

@allenhwkim
Last active April 18, 2018 21:15
Show Gist options
  • Select an option

  • Save allenhwkim/4f87ee848043e88a92d61124b85b369e to your computer and use it in GitHub Desktop.

Select an option

Save allenhwkim/4f87ee848043e88a92d61124b85b369e to your computer and use it in GitHub Desktop.
Simple Lazy Loading With Angular
@Component({
selector: 'ngui-in-view',
template: `
<ng-container *ngIf="inView" [ngTemplateOutlet]="template">
</ng-container>
`,
styles: [':host {display: block;}']
})
export class NguiInViewComponent implements OnInit, OnDestroy {
observer: IntersectionObserver;
inView: boolean = false;
once50PctVisible: boolean = false;
@ContentChild(TemplateRef) template: TemplateRef<any>;
@Input() options: any = { threshold: [.1, .2, .3, .4, .5, .6, .7, .8] };
@Output('inView') inView$: EventEmitter<any> = new EventEmitter();
@Output('notInView') notInView$: EventEmitter<any> = new EventEmitter();
constructor(
public element: ElementRef,
public renderer: Renderer2,
@Inject(PLATFORM_ID) private platformId: any) { }
ngOnInit(): void {
if (isPlatformBrowser(this.platformId)) {
this.observer = new IntersectionObserver(this.handleIntersect.bind(this), this.options);
this.observer.observe(this.element.nativeElement);
}
}
ngOnDestroy(): void {
this.observer.disconnect();
}
handleIntersect(entries, observer): void {
entries.forEach((entry: IntersectionObserverEntry) => {
if (entry.isIntersecting) {
this.inView = true;
this.inView$.emit(entry);
} else {
this.notInView$.emit(entry);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment