我有以下代码:
<ng-container *ngFor="let item of items">
<div>{{item.subArray[0].attr1}}</div>
<div>{{item.subArray[0].attr2}}</div>
</ng-container>
有没有办法为item.subArray [0]创建变量,而不是一遍又一遍地访问第0个元素。例如:
<ng-container *ngFor="let item of items; let subItem = item.subArray[0]">
<div>{{subItem.attr1}}</div>
<div>{{subItem.attr2}}</div>
</ng-container>
答案 0 :(得分:2)
是的,只需嵌套另一个迭代器:
<ng-container *ngFor="let item of items">
<ng-container *ngFor="let subItem of item.subArray">
<div>{{subItem.attr1}}</div>
<div>{{subItem.attr2}}</div>
</ng-container>
</ng-container>