angular 中的多维数组与 *ngFor
multidimensional array in angular with *ngFor
我在 angular
中有一个多维数组
html
<table>
<tr *ngFor="let x of multi">
<td>{{x}}</td>
<td>{{x}}</td>
<td>{{x}}</td>
</tr>
</table>
ts 编辑* - 你不需要我用的
基本上你只需要声明数组
( multi:Number[][] = [[1,2,3],[21,22,23], [31,32,33]]; )
multi:Number[][] = [[1,2,3],[21,22,23], [31,32,33]];
ngOnInit(): void {
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
this.multi[[i][j]];
console.log(this.multi[i][j]);
}
}
}
好吧,我的结果并不是我想要的
1,2,3 1,2,3 1,2,3
21,22,23 21,22,23 21,22,23
31,32,33 31,32,33 31,32,33
这是我想要的
1 2 3
21 22 23
31 32 33
谢谢帮助
您没有迭代第二个维度。试试这个:
<table>
<tr *ngFor="let y of multi">
<td *ngFor="let x of y">{{x}}</td>
</tr>
</table>
我在 angular
中有一个多维数组html
<table>
<tr *ngFor="let x of multi">
<td>{{x}}</td>
<td>{{x}}</td>
<td>{{x}}</td>
</tr>
</table>
ts 编辑* - 你不需要我用的
基本上你只需要声明数组
( multi:Number[][] = [[1,2,3],[21,22,23], [31,32,33]]; )
multi:Number[][] = [[1,2,3],[21,22,23], [31,32,33]];
ngOnInit(): void {
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
this.multi[[i][j]];
console.log(this.multi[i][j]);
}
}
}
好吧,我的结果并不是我想要的
1,2,3 1,2,3 1,2,3
21,22,23 21,22,23 21,22,23
31,32,33 31,32,33 31,32,33
这是我想要的
1 2 3
21 22 23
31 32 33
谢谢帮助
您没有迭代第二个维度。试试这个:
<table>
<tr *ngFor="let y of multi">
<td *ngFor="let x of y">{{x}}</td>
</tr>
</table>