我正在尝试传递要在另一个组件中呈现的组件:
// dynamic-component.ts
export class DynamicComponent {
@Input() content : ObservableContent;
this.render(component) {
this.componentResolver.resolveComponent(component).then(factory => {
let cmp = this.viewContainerRef.createComponent(factory, this.viewContainerRef.length, null, null);
// render code
})
}
ngOnChanges(changes) {
this.render(changes.content.currentValue);
}
}
这是一个输入,content
,它是对实例化组件的引用:
<dynamic-component [content]="dynamicComponent"></dynamic-component>
// code:
export class ComponentWhereDynamicIsLoaded {
...
this.dynamicComponent = new TestComponent();
}
当加载父组件时,dynamicComponent
呈现为字符串[object] [Object]
,这是有道理的,因为我确定Angular将TestComponent
构造函数返回的对象解析为字符串。问题是,如何将TestComponent
正确传递到DynamicComponent
?