基于食谱的example,我是动态创建的组件是这样的:
private loadComponent(): void {
const componentFactory = this.factoryResolver.resolveComponentFactory(MyInputComponent);
const viewContainerRef = this.componentHost.viewContainerRef;
viewContainerRef.clear();
const componentRef = viewContainerRef.createComponent(componentFactory);
(<IComponent>componentRef.instance).data = data;
}
MyInputComponent的模板应该是这样的:
<input type="text" [(ngModel)]="data.inputProp">
我需要在父更新data.inputProp的值,当用户在输入。
我曾经在一些例子中看到了这一点,但不知道它做什么?
componentRef.changeDetectorRef.detectChanges();
我也看到了有关父subscribing to a child's EventEmitter,但只能看到使用的点击事件的例子。什么是用于更新各种数据,包括文本输入回父的更好的方法?
我使用角4 RC3
如果您想将数据发送到动态创建的组件。
this.componentRef.instance.variableName = 'abc'; // here variableName is a variable of dynamic component.
如果你想从动态创建的组件数据。
this.componentRef.instance.observeVariable.subscribe(result => { this.v = result; // here observeVariable is an Observable in dynamic component(ie. this.componentRef).