<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是对象数组
答案 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
并正确分配值。