Skip to content

Instantly share code, notes, and snippets.

@sasxa
Created July 28, 2017 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sasxa/2acae2e6d29af18a315fc4441787ec83 to your computer and use it in GitHub Desktop.
Save sasxa/2acae2e6d29af18a315fc4441787ec83 to your computer and use it in GitHub Desktop.
LoaderDirective
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