【发布时间】:2021-05-03 14:05:18
【问题描述】:
我正在尝试使用slice 在我的页面视图中拆分矩阵的列。如果我slice:0:1,页面只显示第一列。但我不明白为什么如果我尝试选择 slice:1:2 它不会只显示第二列,但仍然只显示第一列。 2:3、3:4 等也一样。
<div class="container">
<tr *ngFor="let row of matrix; index as r">
<td *ngFor="let column of row|slice:0:1; index as c">
<select [(ngModel)]="object[r][c]">
{{r}}{{c}}
</select>
</td>
</tr>
</div>
ngOnInit() {
for (let r = 0; r < 6; r++) {
this.normalizedMatrix.push([0, 0, 0, 0, 0, 0])
this.matrix[r] = []
this.object[r] = {}
this.object[r][r] = 1
for (let c = 0; c < 6; c++) {
if (r == c) {
this.matrix[r].push(1)
this.object[r][c] = 1
}
if (r > c) {
this.matrix[r].push(1 / 9)
this.object[r][c] = 1 / 9
}
else if (r < c) {
this.matrix[r].push(9)
this.object[r][c] = 9
}
}
}
this.onSelectChange(0, 0)
}
【问题讨论】:
-
切片返回什么?听起来它可能会返回已删除的列?你能给我们看一个
matrix值的例子吗? -
@joshvito 我使用 slice 不是删除列,而是因为我想要矩阵值的列表视图。这似乎是最好的解决方案。您认为有更好的方法来查看列出的矩阵的 html 吗?
-
这可能有效,您是否尝试过从
column变量中渲染出一个值?当slice管道的值发生变化时,索引c可能会发生变化。
标签: html typescript matrix multidimensional-array slice