Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/399.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
Javascript angular 2中有多个复选框_Javascript_Angularjs_Angular_Checkbox - Fatal编程技术网

Javascript angular 2中有多个复选框

Javascript angular 2中有多个复选框,javascript,angularjs,angular,checkbox,Javascript,Angularjs,Angular,Checkbox,在组件中,我有定义复选框 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, firstNam

在组件中,我有定义复选框

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>

{{screery.name}

问题是,当我选中一个复选框时,所有其他复选框都被选中。我很困惑,没有在轨道上实现这个复选框功能

将多个复选框绑定到同一个
user.groceries

将它们绑定到属性
(迭代变量
*ngFor
),或者创建另一个数组,将每个复选框绑定到不同的数组条目

  <span *ngFor="let grocery of groceries">
    <label>
    <input type="checkbox" name="groceries" [(ngModel)]="grocery.isChecked" 
    {{grocery.name}}
    </label>
  </span>


您将多个复选框绑定到同一
user.groceries

将它们绑定到属性
(迭代变量
*ngFor
),或者创建另一个数组,将每个复选框绑定到不同的数组条目

  <span *ngFor="let grocery of groceries">
    <label>
    <input type="checkbox" name="groceries" [(ngModel)]="grocery.isChecked" 
    {{grocery.name}}
    </label>
  </span>


杂货中是否存在任何问题:字符串?bcz如果我选择多个杂货,它将如何提交?我不知道“在模型中,我将杂货制作为字符串”意思是。你的问题没有显示任何关于表单的内容,因此我不明白这与提交有什么关系。基本上我想用逗号分隔的方式提交,但不知道如何提交。如果你想让我提供指导,你需要提供更具体的信息。请编辑你的问题并添加代码,以演示你不需要的内容ry完成。杂货中是否存在任何问题:字符串?bcz如果我选择多个杂货,它将如何提交?我不知道“在模型中,我将杂货作为字符串制作”意思是。你的问题没有显示任何关于表单的内容,因此我不明白这与提交有什么关系。基本上我想用逗号分隔的方式提交,但不知道如何提交。如果你想让我提供指导,你需要提供更具体的信息。请编辑你的问题并添加代码,以演示你不需要的内容我很难完成。
  <span *ngFor="let grocery of groceries; let i=index">
    <label>
    <input type="checkbox" name="groceries" [(ngModel)]="checkedGroceries[i]" 
    {{grocery.name}}
    </label>
  </span>