角度5中的多选复选框值

时间:2018-07-18 07:43:30

标签: angular typescript angular5

<div *ngFor="let user_id of filterdUsers; let i = index"> 
   <input type="checkbox" name="something{{i}}" class="something{{i}}"  [checked]= "something === model.user_id"  [(ngModel)]="model.user_id" [value] = "user_id._id" (click)="onCheckClick($event, user_id)"> {{user_id.username}} 
</div>

我正在运行时生成复选框。当我单击复选框时,所有复选框均被选中,并在单击单个复选框时获得多个复选框的值。在上面的代码中,filterdUsers是对象数组

1 个答案:

答案 0 :(得分:0)

这听起来像是trackBy

可以解决的问题
<div *ngFor="let user_id of filterdUsers; let i = index; trackBy: trackByFn"> 
   <input type="checkbox" name="something{{i}}" class="something{{i}}"  [checked]= "something === model.user_id"  [(ngModel)]="model.user_id" [value] = "user_id._id" (click)="onCheckClick($event, user_id)"> {{user_id.username}} 
</div>

然后将功能添加到您的组件中

trackByFn(i: number) {
    return i;
  }

这使angular可以智能地索引ngFor并正确分配值。