在组件中我定义了复选框
public groceries = [
{ name: 'Seitan', bought: false },
{ name: 'Almond Meal Flour', bought: false },
{ name: 'Organic Eggs', bought: false }
];
在模型中,我将杂货作为字符串
export class User {
id: number,
email: string,
password: string,
firstName: string,
lastName: string,
groceries: string,
}
component.html
<span *ngFor="let grocery of groceries">
<label>
<input type="checkbox" name="groceries" [(ngModel)]="user.groceries"
[value]="grocery.name">
{{grocery.name}}
</label>
</span>
问题在于,当我选择一个复选框时,所有其他复选框都被选中。我很困惑,并没有按计划实现此复选框功能。