我正试图使用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
。 - user47589