2

我正在尝试动态创建要在 Golden Layout 中注册的组件。例如,

    @ViewChild('placeholder', { read: ViewContainerRef }) viewContainerRef;

ngAfterViewInit(){
    this.myLayout.registerComponent('testComponent', (container, componentState) => {
        //Create an empty div on the container

        container.getElement().html("<div #placeholder></div>");
        this.componentFactory = this.componentFactoryResolver.resolveComponentFactory(TestComponent);
        this.cmpRef = this.viewContainerRef.createComponent(this.componentFactory);
        this.cmpRef.changeDetectorRef.detectChanges();
        GlDirective.componentCount++;
    });
    }

但是,因为 viewContainerRef 指的是现在才创建的 ViewChild,所以它始终是未定义的。我们如何在 RC5 中创建一个像上面那样使用动态添加的 div 的组件。我使用@Günter Zöchbauer 对Angular 2 动态选项卡的回答以及用户单击选择的组件来得出此结论。但是不确定如何使用需要动态生成 DOM 的 Golden Layout 来实现这一点。请帮忙。

4

1 回答 1

0

这是不支持的。

#placeholder

container.getElement().html("<div #placeholder></div>");

只会被添加到浏览器中,Angular2 不会以任何方式识别或处理(出于安全目的的清理除外)。

模板变量只能通过将其静态添加到组件模板来创建。

于 2016-08-19T15:32:03.140 回答