Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何在ngFor中获取复选框值?在角度2+;?_Angular_Checkbox_Angular Ngmodel_Ngfor - Fatal编程技术网

Angular 如何在ngFor中获取复选框值?在角度2+;?

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

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="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);
    }
  }