npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ng-lazy-load

v1.0.8

Published

A library to enable lazy loading offscreen resource files

Downloads

131

Readme

ng-lazy-load

npm npm License

ng-lazy-load library provides lazyLoad directive and LazyLoadService to lazy load your resource file when it is near to be shown on screen. This library is an extension of @trademe/ng-defer-load to allow more control on its behaviour and make it useful in ngFor loop. The library will output debug logs to console in the development mode to show what's happening inside the library:

...
lazyLoad never
lazyLoad register
loading [0] (Intersecting)
loading [1] (NearIntersecting 0)
loading [2] (NearIntersecting 0)
loading [5] (Intersecting)
...

Usage

Simplest usage would be:

<div (lazyLoad)="toLoad=true">
  <img [src]="toLoad ? url : ''">
</div>

A typical usage for ngFor loop would be:

<my-element #myElement
    *ngFor="let item of items$ | async; let i = index"
    (lazyLoad)="myElement.doLoad($event)" [url]="item.url" [index]="i" >
</my-element>
// in my-element component
import { IntersectionState } from 'ng-lazy-load';

doLoad(state: IntersectionState) {
  this.toLoad = true;
}

By having additional [url] and [index] inputs, the directive can choose to register the api only for elements that has a resource to load and provide some flexibility in its behaviour to cope with various needs.

LazyLoadService.loadAheadCount

Default value of LazyLoadService.loadAheadCount property is 2 which means that if [n]th element is intersecting, [n+1]th and [n+2]th elements will also be loaded as well. You can override this by setting to other value:

// in parent component
import { LazyLoadService } from 'ng-lazy-load';

constructor(private lazyLoadService: LazyLoadService) { }

ngOnInit() {
  this.lazyLoadService.loadAheadCount = 5; // load 5 elements ahead beyond the intersecting one

LazyLoadService.registerAfter()

By default, lazyLoad directive will register the IntersectionObserver api on its host element's init. But you may want to delay its registration:

// in parent component
ngOnInit() {
  this.lazyLoadService.registerAfter(1500); // let directives register after 1500 msec 

LazyLoadService.registerLater() and LazyLoadService.registerAll()

Or let directives register later when asked by the parent component:

// in parent component
ngOnInit() {
  this.lazyLoadService.registerLater(); // let directives not register on init
}
// somewhere in parent component
this.lazyLoadService.registerAll(); // let directives register now

A Stackblitz demo is available to show the usage.

Installation

// to install
npm install ng-lazy-load

// in app.module.ts
import { LazyLoadModule } from 'ng-lazy-load';
@NgModule({
  imports: [
    LazyLoadModule,

// in template
<div (lazyLoad)="toLoad=true">
  <img [src]="toLoad ? url : ''">