Skip to content

Instantly share code, notes, and snippets.

@jdnielss
Created August 23, 2021 17:37
Show Gist options
  • Save jdnielss/a44138f8ac1dd8db3d1121bd4b3a11c9 to your computer and use it in GitHub Desktop.
Save jdnielss/a44138f8ac1dd8db3d1121bd4b3a11c9 to your computer and use it in GitHub Desktop.
Lazy Loading Directive Angular
import { Directive, ElementRef } from '@angular/core';
@Directive({ selector: 'img' })
export class LazyImgDirective {
constructor({ nativeElement }: ElementRef<HTMLImageElement>) {
const supports = 'loading' in HTMLImageElement.prototype;
if (supports) {
nativeElement.setAttribute('loading', 'lazy');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment