【发布时间】:2017-02-18 11:40:06
【问题描述】:
我正在尝试构建一个动态附加另一个组件的组件。例如,这里是我的父类:
import { Component, ComponentRef, ViewChild, ViewContainerRef, ComponentFactoryResolver } from '@angular/core';
@Component({
templateUrl: './app/sample-component.component.html',
selector: 'sample-component'
})
export class SampleComponent {
@ViewChild('dynamicContent', { read: ViewContainerRef })
protected dynamicComponentTarget: ViewContainerRef;
private currentComponent: ComponentRef<any>;
private selectedValue: any;
constructor(private componentResolver: ComponentFactoryResolver) {
}
private appendComponent(componentType: any) {
var factory = this.componentResolver.resolveComponentFactory(componentType);
this.currentComponent = this.dynamicComponentTarget.createComponent(factory);
}
}
sample-component.component.html:
<div #dynamicContent></div>
它可以很好地附加一个元素,但我不知道如何动态绑定双向,就像我在静态组件中所做的那样:[(ngModel)]="selectedValue"
【问题讨论】: