数组的数组上的循环——Angular



我从服务中获得数组数组,我想在每次迭代中显示case[0]和[1],但是当我这样做时,我得到一个错误TypeError:无法读取未定义的属性(读取'0')

<ng-container
*ngFor="let done of this.launch.doneAreas; index as i">
<div class="display-result">
Case n° {{ i + 1 }} => ({{ done[i][0] }},{{ done[i][1] }})
</div>
</ng-container>

数组在服务中定义如下:

doneAreas: any[][] = [];

,我通过压入一组数字来压入值。坐标是由两个数字组成的数组

this.doneAreas.push(this.coords);

这是一个我想在console.log中显示的数组的例子。

0: (2) [3,3]

1: (2) [3,4]

2: (2) [4,4]

3:数组(2)0: 41: 3长度:2[[原型]]:数组(0)

感谢

可以创建一个动态对象{x: number, y: number}。如果你在列表中使用它,你可以确保两者都应该出现。

您的问题是done already是一个对象,而您正在尝试获取一个不存在的数组的索引。

doneAreas: {x: number, y: number}[] = [];
this.doneAreas.push({this.coords.x, this.coords.y);
<ng-container
*ngFor="let done of this.launch.doneAreas; index as i">
<div class="display-result">
Case n° {{ i + 1 }} => ({{ done.x }},{{ done.y }})
</div>
</ng-container>

如果你想让代码保持原样,你应该删除[I]:

<ng-container
*ngFor="let done of this.launch.doneAreas; index as i">
<div class="display-result">
Case n° {{ i + 1 }} => ({{ done[0] }},{{ done[1] }})
</div>
</ng-container>

最新更新