JS数组增删改方法 splice() 根据索引删除元素
<li
*ngFor="let todo of todos; let i = index;"
[ngClass]="{completed: todo.done}"
>
<button
class="destroy"
(click)='removeTodo(i)'
></button>
</li>
removeTodo(index: number){
this.todos.splice(index, 1)
console.log(index);
}
splice()
函数,可以对数组实现增删改操作- splice(索引,删除或修改的数目,添加项,...)
- 增:splice(索引,0,添加项,...)
- 删:splice(索引,删除的数量)
- 改:splice(索引,连续修改的数目,新加项)