【发布时间】:2018-03-23 14:12:05
【问题描述】:
我正在尝试克隆我的数组,然后使用 slice() 函数从中删除一个元素。但是,每当我单击要删除的元素时,它都会删除数组中的所有内容,但我单击的元素除外。
这是我当前的代码:
deleteContact(contacts: Contacts){
if (contacts === null || contacts === undefined) {
return;
}
const pos = this.contacts.indexOf(contacts);
if (pos < 0) {
return;
}
this.contacts = this.contacts.splice(pos, 1);
this.contactsListClone = this.contacts.slice();
this.contactListChangedEvent.next(this.contactsListClone);
}
【问题讨论】:
-
您正在从
contacts在克隆它之前删除除一个之外的所有项目。另外,您的问题说您使用的是slice,但您的代码使用的是splice
标签: javascript arrays angularjs