我正在尝试从二维对象数组中打印一个表格,其中包含一个属性“文本”。它只打印表格行,遍历字段不起作用。
我的 component.html 看起来像这样:
<section *ngIf="object">
<table>
<tr *ngFor="let row of array; let even = even; let odd = odd"
[ngClass]="{ odd: odd, even: even }">
<td class="field" *ngFor="let field of array[row]">
{{field.text}}
</td>
</tr>
</table>
</section>
数组:object[][] 已正确填充,我可以将“文本”属性记录到控制台。问题是:我不知道如何遍历第二个维度 (*ngFor="let field of array[row]"
)
最佳答案
假设array
是一个数组数组,
你的第二个 ngFor
应该是 *ngFor="let field of row"
您不能使用 array[row]
,因为 row
包含二维数组而不是索引。
<section *ngIf="object">
<table>
<tr *ngFor="let row of array; let even = even; let odd = odd"
[ngClass]="{ odd: odd, even: even }">
<td class="field" *ngFor="let field of row">
{{field.text}}
</td>
</tr>
</table>
</section>
关于html - 如何从 Angular 5 中的二维数组打印表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47775188/