请点击这个 StackBlitz 链接(不可用
)
请点击这个 StackBlitz 链接(正常工作
)
我的问题与 Array.splice
有关。当数组数据不同的时候,Array.splice
可以正常工作,但是当数组数据相同的时候,Array.splice
只会删除最后一个索引,并忽略提供的索引以从数组中删除元素。
两个示例都共用相同的 HTML 模板
<div class="container">
<table class="table table-dark table-striped table-hover">
<thead>
<tr>
<td scope="col"> Index </td>
<td scope="col"> Value </td>
<td scope="col"> Action </td>
</tr>
</thead>
<tbody>
<tr *ngFor="let row of rows; let i = index" [@itemAnim]>
<td> {{i}} </td>
<td>{{row}}</td>
<td><button class="btn btn-danger" (click)="deleteRow(i)">Delete</button></td>
</tr>
</tbody>
</table>
<button class="btn btn-primary " (click)="addRow()">Add row</button>
</div>
看这个例子按预期工作:
index = 1;
rows = [1];
addRow() {
this.rows.length > 0 ? this.index ++ : this.index = 1;
this.rows.push(this.index)
}
deleteRow(row: number) {
return this.rows.length > 0 ? this.rows.splice(row, 1) : this.index = 1;
}
这个无法工作:
rows = [1];
addRow() {
this.rows.push(1)
}
deleteRow(row: number) {
this.rows.splice(row, 1) ;
}
在上述代码中,当单击“addButton”时,我只推入
1
。而且删除按钮仅从最后一个索引中进行删除,而不提供行号索引。
我的问题是为什么会发生这种情况? 上面的stackBlitz链接显示了相同的模糊性。
splice
中不起作用。 - DeveloperdeleteRow
函数?虽然引用外部网站的链接可能会有所帮助,但请确保所有相关信息都在问题中,而不是在链接后面。 - trincot