Created
July 28, 2017 14:10
-
-
Save sasxa/2acae2e6d29af18a315fc4441787ec83 to your computer and use it in GitHub Desktop.
LoaderDirective
This file contains 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 { | |
ComponentFactoryResolver, | |
Directive, | |
Input, | |
OnInit, | |
Output, | |
ViewContainerRef, | |
ComponentRef, | |
EventEmitter | |
} from '@angular/core'; | |
// tslint:disable-next-line:directive-selector | |
@Directive({ selector: '[loadComponent]' }) | |
export class LoaderDirective implements OnInit { | |
// tslint:disable-next-line:no-input-rename | |
@Input('loadComponent') | |
component: any; | |
@Output() | |
loaded: EventEmitter<ComponentRef<any>> = new EventEmitter(); | |
constructor( | |
protected cfRef: ComponentFactoryResolver, | |
protected vcRef: ViewContainerRef, | |
) { } | |
/* istanbul ignore next */ | |
ngOnInit() { | |
if (!this.component) { return; } | |
const componentFactory = this.cfRef.resolveComponentFactory(this.component); | |
const componentRef = this.vcRef.createComponent(componentFactory); | |
this.loaded.emit(componentRef); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment