【发布时间】:2019-02-18 08:50:55
【问题描述】:
目前,我可以像这样动态加载单个组件并将其显示在 ViewChild 中:
@Component({
selector: '...',
template: '<ng-container #viewRef></ng-container>'
})
export class SomeComponent {
@ViewChild('viewRef', {read: ViewContainerRef}) public viewRef;
constructor(private compiler: Compiler) {}
ngAfterViewInit() {
this.compiler.compileModuleAndAllComponentsAsync(SomeModule).then((factory) => {
this.componentRef = this.placeholder.createComponent(factory);
});
}
}
现在我想加载多个组件并在列表中动态显示它们。 ViewChildren 应该是解决这个问题的方法。问题是,ViewChildren 不允许添加新元素或在ViewChild 上创建类似于createComponent 的元素。
如何创建动态组件并将它们添加到ViewChildren?
【问题讨论】:
标签: angular