使用 Angular 5,我创建了一个页面,用户可以在其中使用复选框进行选择。当检查项目时,我将它们的ID
s添加到一个数组selecteddocs
. 当它们未选中时,我想从数组中删除该项目。当检测到复选框未选中时,如何触发删除项目的功能?
这是 HTML:
<table>
<tr>
<th></th>
<th>Document Group</th>
<th>Title</th>
<th>Description</th>
</tr>
<tr *ngFor="let doc of docs">
<td><input type="checkbox" (click)="select(doc.ID)"></td>
<td>{{doc.DocumentGroup}}</td>
<td>{{doc.Title}}</td>
<td>{{doc.Description}}</td>
</tr>
</table>
这是TS:
selectedqr;
selecteddocs = [];
select(ID){
this.selectedqr = ID;
console.log(this.selectedqr);
this.selecteddocs.push(ID);
console.log(this.selecteddocs);
}
//On Uncheck: var index = selecteddocs.indexOf(ID);
selecteddocs.splice(index, 1);
只需传入更改事件复选框的值即可。
<td><input type="checkbox" [(ngModel)]="doc.selected" (change)="select(doc.ID, doc.selected)"></td>
然后根据 doc.selected 为您提供的内容运行适当的功能。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句