Created
August 23, 2021 17:37
-
-
Save jdnielss/a44138f8ac1dd8db3d1121bd4b3a11c9 to your computer and use it in GitHub Desktop.
Lazy Loading Directive Angular
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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