【发布时间】:2019-08-02 13:24:38
【问题描述】:
目前我正在将我的应用程序从 Angular JS 升级到 Angular 8。我想使用 ngFor 动态呈现多个组件。
我有多个 UI 组件,它们本质上是相似的。我想遍历并显示组件。在 Angular JS 中,我可以做到 这使用 ng-repeat 和 ng-include 基于从数据传递的模板 url。这适用于 AngularJS
但在 Angular 中,我们没有 ng-include。所以我尝试使用开关盒,但将来可以添加新组件。所以我想根据数据使其动态化。
Angular JS
<li ng-repeat="component in ui.componentArray">
<div ng-include="templateUrl" dynamicController="config.controller"></div>
</li>
组件数组包含可变数量的 html 组件,如 Component1、Component2、Component3 等,以及 ComponentController1、ComponentController3、ComponentController3 等控制器。
Angular 8 我尝试使用 switch case,但它是静态的。我想让它动态化,因为我们可以有更多的组件。
角度 8
<div [ngSwitch]="component.name">
<app-component1 *ngSwitchCase="'component1'"></app-component1>
<app-component2 *ngSwitchCase="'component2'"></app-component2>
<app-component3 *ngSwitchCase="'component3'"></app-component3>
</div>
如何在 Angular 8 中实现上述代码。
【问题讨论】:
-
为什么不只是 *ngIf 并控制来自 ts 代码的值。
标签: angular