ng-defer-load is an Angular directive to load elements lazily.
It uses Intersection Observer API to check if an element is in viewport and falls back to scroll detection mechanism for unsupported browsers.
Using npm:
$ npm i @trademe/ng-defer-load-
Import
DeferLoadModuleinto the module corresponding to your component -
Use the directive with the element you wish to lazy load
<div
(deferLoad)="showMyElement=true">
<my-element
*ngIf=showMyElement>
...
</my-element>
</div>Note: You might want to have a loading state for your element with approximately same height as the element.
Demo of ng-defer-load in use is available here.
Released under the MIT license.