【发布时间】:2020-08-23 11:31:46
【问题描述】:
首先,这与以下问题有关:
Is there a way to programmatically render a component in Angular?
是否可以从视图模型中的数组渲染动态组件? (类似于对上述问题中的单个组件使用 ng-template)。
<div *ngFor="let component of components">
<Do something here to render component>
</div>
我之前使用过 KnockoutJs,并且在视图中使用类似以下内容很容易做到这一点:
<!-- ko foreach: someArrayOfComponents -->
<!-- ko component: { componentName: 'some-component', params: someComponentViewModel } --><!-- /ko -->
<!-- /ko -->
【问题讨论】:
-
文档并没有真正提到迭代动态组件。它似乎只谈论组件何时被专门键入。但是,这可能吗:
-
组件数组会是什么样子? Angular 中的组件需要使用 componentFactory 创建。你想要的很可能,但它需要更多的功能。
-
我不知道组件属性在视图模型上的外观。我看到你可以像这样注入 ViewChildren:
@ViewChildren(Item) items:QueryList<Item>;但是 QueryList 对象有一个通用类型参数(因为我要求任何组件,我不确定它是如何工作的)
标签: angular angular-components