angular6 - 如何使用 angular + 从数组中删除列表
问题描述
我正在使用复选框来获取以下列表是数据:
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);
}
}
现在列表正在添加到数组中..
但是当取消选择复选框时,我想从数组中删除列表。有谁能够帮我??
解决方案
使用拼接功能
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.com/edit/angular-splice
如需更多说明:https ://love2dev.com/blog/javascript-remove-from-array/
推荐阅读
- c# - 康托索大学
- lwip - tcp_write 和 tcp_output 函数的区别
- prolog - Prolog 约束逻辑编程 (clpr) 中的未知数顺序
- javascript - 如何通过循环禁用所有输入框
- c - 为什么我的 Mac 上的任何 C 编译器都没有定义 timespec_get?
- javascript - 为什么滑块只适用于两张幻灯片?
- css - 如何将两个 div 并排放置,其中一个包含根据另一个 div 的大小缩放的图像?
- mysql - Docker + Rails + MySQL = 忽略 database.yml 中的环境变量
- java - 使用 MQT 合并数据,对性能的影响为零
- python - python 3在'int和'str'的实例之间不支持错误类型错误'<'