Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add inputs to dynamic component
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 {
name = 'Dynamic';
@ViewChild('container', { read: ViewContainerRef }) container: ViewContainerRef;
constructor (private resolver: ComponentFactoryResolver) {
}
addComponent() {
let factory = this.resolver.resolveComponentFactory(DynamicComponent);
this.container.createComponent(factory);
let inputs = { name: this.name };
let component = factory.create(null);
Object.keys(inputs).map(input => {
component.instance[input] = inputs[input];
});
this.container.insert(component.hostView);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.