* ngFor中的条件

时间:2016-09-14 08:01:42

标签: angular angular2-template

我有两个字符串数组,我想迭代其中一个取决于条件。如何避免代码重复?现在*ngIf现在:

<tr *ngIf="!condition">
    <td *ngFor="let field of firstArray">{{field}}</td>
</tr>
<tr *ngIf="condition">
    <td *ngFor="let field of secondArray">{{field}}</td>
</tr>

有更好的方法吗?

1 个答案:

答案 0 :(得分:7)

<tr>
    <td *ngFor="let field of (condition ? secondArray : firstArray)">{{field}}</td>
</tr>
相关问题