text
stringlengths 1
80.5k
⌀ |
---|
] |
}) |
export class LazyModule { |
// ... |
} |
... but, here's where Angular's dependency-injection gets really exciting: any element in the component tree can override the LazyViewport provider, thereby causing descendant [lazySrc] instances to be associated with a more local viewport. To facilitate overriding of the LazyViewport service in a subtree of the component graph, I created a [lazyViewport] directive: |
// Import the core angular services. |
import { Directive } from "@angular/core"; |
import { ElementRef } from "@angular/core"; |
import { OnDestroy } from "@angular/core"; |
import { OnInit } from "@angular/core"; |
// Import the application components and services. |
import { LazyViewport } from "./lazy-viewport.ts"; |
// ----------------------------------------------------------------------------------- // |
// ----------------------------------------------------------------------------------- // |
@Directive({ |
selector: "[lazyViewport]", |
inputs: [ "offset: lazyViewportOffset" ], |
// The primary role of this directive is to override the default LazyViewport |
// instance at this point in the component tree. This way, any lazy-directives |
// that are descendants of this element will receive this instance when using |
// dependency-injection. |
providers: [ |
{ |
provide: LazyViewport, |
useClass: LazyViewport |
} |
] |
}) |
export class LazyViewportDirective implements OnInit, OnDestroy { |
public offset: number; |
private elementRef: ElementRef; |
private lazyViewport: LazyViewport; |
// I initialize the lazy-viewport directive. |
constructor( |
elementRef: ElementRef, |
lazyViewport: LazyViewport |
) { |
this.elementRef = elementRef; |
this.lazyViewport = lazyViewport; |
this.offset = 0; |
} |
// --- |
// PUBLIC METHODS. |
// --- |
// I get called once when the directive is being destroyed. |
public ngOnDestroy() : void { |
this.lazyViewport.teardown(); |
} |
// I get called once after the inputs have been bound for the first time. |
public ngOnInit() : void { |
// Ensure that the offset value is numeric when we go to initialize the viewport. |
if ( isNaN( +this.offset ) ) { |
console.warn( new Error( `[lazyViewportOffset] must be a number. Currently defined as [${ this.offset }].` ) ); |
this.offset = 0; |
} |
// Now that this LazyViewport directive has overridden the instance of |
// LazyViewport in the dependency-injection tree, we have to initialize it |
// to use the current element as the observer root. |
this.lazyViewport.setup( this.elementRef.nativeElement, +this.offset ); |
} |
} |
As you can see, this directive defines a providers collection which overrides the injectable service associated with the LazyViewport dependency-injection token. By doing this, any [lazySrc] directive lower down in the component tree will get this LazyService override-instance rather than the global one: |
this.lazyViewport.setup( this.elementRef.nativeElement, +this.offset ); |
At this time, I think it makes more sense to always go with the global instance. But, I just love how Angular's dependency-injection tree makes it so easy to change the viewport. Dependency-injection is just such a win! |
Once I import the LazyModule into my AppModule (not worth showing), I can then use the [lazySrc] directive on my IMG tags. In this case, I've created a scrolling list of contacts with avatars. The avatars will only load when the contact is scrolled into view: |
// Import the core angular services. |
import { Component } from "@angular/core"; |
// ----------------------------------------------------------------------------------- // |
// ----------------------------------------------------------------------------------- // |
interface Contact { |
id: number; |
name: string; |
avatarUrl: string; |
} |
@Component({ |
selector: "my-app", |
styleUrls: [ "./app.component.less" ], |
template: |
` |
<p> |
<a (click)="toggleContacts()">Toggle Contacts</a> |
</p> |
<ul *ngIf="isShowingContacts" class="contacts"> |
<li *ngFor="let contact of contacts"> |
<img [lazySrc]="contact.avatarUrl" lazySrcVisible="visible" /> |
<span>{{ contact.name }} - {{ contact.id }}</span> |
</li> |
</ul> |
<p> |
<a (click)="popContact()">Pop Contact</a> |
— |
<a (click)="pushContact()">Push Contact</a> |
</p> |
` |
}) |
export class AppComponent { |
public contacts: Contact[]; |
public isShowingContacts: boolean; |
public maxID: number; |