Angular 如何在ngFor中获取复选框值?在角度2+;?
Hy,我需要通过ngFor中的复选框来收集所选值。下面是代码Angular 如何在ngFor中获取复选框值?在角度2+;?,angular,checkbox,angular-ngmodel,ngfor,Angular,Checkbox,Angular Ngmodel,Ngfor,Hy,我需要通过ngFor中的复选框来收集所选值。下面是代码 <tr *ngFor="let item of items; let index = index;"> <td>{{item.bed}}</td> <td>{{item.size}}</td> <td>{{item.name}}</td> <td> <input type="checkbo
<tr *ngFor="let item of items; let index = index;">
<td>{{item.bed}}</td>
<td>{{item.size}}</td>
<td>{{item.name}}</td>
<td>
<input type="checkbox"
name="domain-{{item.bed}}"
[(ngModel)]="items[index].id"
>
</td>
</tr>
<button (click)="OnSelect()">Select</button>
在选择时,我会看到打印的项目的所有值,而不是选中的复选框值。有什么想法吗
<td>
<input type="checkbox"
name="item-{{item.bed}}"
#myItem
(change)="OnCheckboxSelect(item.id, $event)"
>
</td>
我能通过这种方式得到我想要的
我能通过这种方式得到我想要的
<td>
<input type="checkbox"
name="item-{{item.bed}}"
#myItem
(change)="OnCheckboxSelect(item.id, $event)"
>
</td>
@ViewChildren('myItem') item;
selectedIds = [];
OnCheckboxSelect(id, event) {
if (event.target.checked === true) {
this.selectedIds.push({id: id, checked: event.target.checked});
console.log('Selected Ids ', this.selectedIds);
}
if (event.target.checked === false) {
this.selectedIds = this.selectedIds.filter((item) => item.id !== id);
}
}