Status in * ngFor

I have two string arrays, and I want the iteration through one of them to depend on the state. How to avoid code duplication? Now *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>

Is there a better way?

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

Source: https://habr.com/ru/post/1654538/


All Articles