Skip to content

Instantly share code, notes, and snippets.

@spali
Created December 6, 2016 11:59
Show Gist options
  • Save spali/91ec412d9b321cdd3c9d9da00e2f56ed to your computer and use it in GitHub Desktop.
Save spali/91ec412d9b321cdd3c9d9da00e2f56ed to your computer and use it in GitHub Desktop.
Dynamic Module with dynamic Component in Angular 2
import { Component, OnInit, ViewContainerRef, ComponentRef, Compiler, NgModule, OnDestroy } from '@angular/core';
@Component({
moduleId: module.id,
selector: 'dynamic-container',
templateUrl: 'static.component.html'
})
export class DynamicContainerComponent implements OnInit, OnDestroy {
protected componentRef: ComponentRef<any>;
constructor(private vcRef: ViewContainerRef, private compiler: Compiler) {
}
ngOnInit() {
this.destroy();
var component = this.createDynamicComponent('<h1>dynamic Content</h1>');
var module = this.createDynamicModule(component);
this.compiler.compileModuleAndAllComponentsAsync(module)
.then(moduleWithComponentFactories => {
var componentFactory = moduleWithComponentFactories.componentFactories.find(element => element.componentType === component);
this.componentRef = this.vcRef.createComponent(componentFactory);
});
}
ngOnDestroy() {
this.destroy();
}
private destroy() {
if (this.componentRef) {
this.componentRef.destroy();
}
}
private createDynamicComponent(template: string) {
@Component({
selector: 'dynamic-content',
template: template
})
class DynamicComponent { };
return DynamicComponent;
}
private createDynamicModule(component: any) {
@NgModule({
declarations: [component]
})
class DynamicModule { };
return DynamicModule;
}
}
@spali
Copy link
Author

spali commented Dec 7, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment