如何将组件的引用传递给另一个组件?

时间:2016-08-22 16:23:54

标签: angularjs angular

我正在尝试传递要在另一个组件中呈现的组件:

// 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

0 个答案:

没有答案