小编Inn*_*pts的帖子

angular2复选框formcontrol

对于带有绑定到FormControl的字符串值的简单复选框:

export class CheckboxComponent {
  formControl: FormControl;
  option: {value: string};

  constructor() {
    this.formControl = new FormControl(); 
    this.option = {value: "MyValue"};

         this.formControl.valueChanges.subscribe(console.log);
  }

}
Run Code Online (Sandbox Code Playgroud)
<input type="checkbox" [formControl]="formControl" [value]="option.value" name="SomeName" />
Run Code Online (Sandbox Code Playgroud)

subscribe的输出是true,false,true,false ....我希望angular 2绑定FormControl中的字符串值"MyValue".

默认情况下,角度2,FormControl和复选框似乎绑定布尔值,这是奇怪的,因为默认浏览器提交行为是发送复选框的值,并且在多个具有相同名称的复选框的情况下,绑定到的复选框值的数组复选框的名称.

提交将毫无用处:

Items=[true, false, true, true, true, false]
Run Code Online (Sandbox Code Playgroud)

代替:

Items=[Toothbrush, Floss, Glock, Clean underwear]
Run Code Online (Sandbox Code Playgroud)

所以本质上:如何使angular 2绑定字符串值而不是布尔值?

亲切的问候,

checkbox angular2-forms angular

9
推荐指数
1
解决办法
1万
查看次数

标签 统计

angular ×1

angular2-forms ×1

checkbox ×1