Skip to content

Instantly share code, notes, and snippets.

@natmegs
Created December 20, 2017 23:15
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 natmegs/67dfef1eeab82181b0d9da2cc2c102e9 to your computer and use it in GitHub Desktop.
Save natmegs/67dfef1eeab82181b0d9da2cc2c102e9 to your computer and use it in GitHub Desktop.
Create addComponent method
import { Component } from '@angular/core';
import { DynamicComponent } from './dynamic.component';
@Component({
selector: 'host',
templateUrl: './host.component.html',
styleUrls: [ './host.component.css' ],
entryComponents: [DynamicComponent]
})
export class AppComponent {
@ViewChild('container', { read: ViewContainerRef }) container: ViewContainerRef;
constructor (private resolver: ComponentFactoryResolver) {
}
addComponent() {
let factory = this.resolver.resolveComponentFactory(DynamicComponent);
let component = factory.create(null);
this.container.insert(component.hostView);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment