如何使用angular +从数组中删除列表。

问题描述 投票:0回答:1

我是用复选框得到下面的数据列表。

data:[{ email: "email1" }, { email: "email2" }, { email: "email3" }, { email: 'email4' }]

html

<table>
  <thead>
     <th></th>
     <th></th>
   </thead>  
   <tbody>
     <td *ngFor="let x of collection;">              
         <input type="checkbox" (change)="checkEmails($event,x)" />
     </td>
     <td>{{x.email}}</td>
   </tbody>
<table>

.ts文件

public groupList: any = [];
checkEmails(event, email) {
    if (event.target.checked) {
      if (this.groupList.length == 0) {
        this.groupList.push(email);
      }
      else {
        this.groupList.push(email);
      }
      console.log("bulkData", this.groupList);
    }
  }

现在列表添加到一个数组中,但当取消选择复选框时,我想从数组中删除列表。

但是当取消选择复选框时,我想把列表从数组中删除,有人能帮帮我吗?

enter image description here

angular6 angular5 angular7
1个回答
0
投票

使用 拼接 功能

checkEmails(event, email) {
if (event.target.checked) {
  this.groupList.push(email);
  console.log("bulkData", this.groupList);
} else {
  for(var i = 0 ; i < this.groupList.length; i++) {
    if(this.groupList[i].email == email.email) {
      this.groupList.splice(i, 1);
    }
  }
  console.log("bulkData1", this.groupList);
}

}

工作实例。https:/stackblitz.comeditangular-splice。

更多的澄清。https:/love2dev.comlogjavascript-remove-from-array。

© www.soinside.com 2019 - 2024. All rights reserved.