2016-09-14 45 views
4

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

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

有没有更好的方法?

回答

6
<tr> 
    <td *ngFor="let field of (condition ? secondArray : firstArray)">{{field}}</td> 
</tr> 
+1

呵呵,我想过这样。我必须尝试!无论如何,谢谢。这正是我需要的。 –