【发布时间】:2019-01-27 17:18:52
【问题描述】:
I have to handle multiple checkbox in reactive form with selection options as "W9", "F9", "other" and when "other" is selected, then a text box should get open, also required validation is needed for this .当我提交这个时,我应该得到输出为 {"Role":{W9:true, other:true,otherValue:value}}
ngOnInit() {
this.customerForm = this.fb.group({
Roles: this.buildRoles()
})
buildRoles() {
const arr = this.roles.map(role => {
return this.fb.control(false);
});
console.log(this.fb.array(arr));
return this.fb.array(arr);
}
get role() {
return <FormArray>this.customerForm.get('roles');
}
HTML 文件
<label>Role</label>
<div *ngFor="let control of role.controls; let i = index;">
<input type="checkbox"
[formControl] = "control">
<label>{{roles[i]}}</label>
我只能根据复选框的选择获得真假数组,而没有其他值。
【问题讨论】:
-
请创建一个展示此问题的 StackBlitz。
标签: angular angular-reactive-forms