我试图在选中时为复选框指定一个值,而在取消选中时没有(或false
):
<input type="checkbox" [value]="key" [formControlName]="variable"/>
这不起作用,值为true
或false
。是否可以分配变量?
答案 0 :(得分:3)
你可以试试这个:
<input type="checkbox" (change)="$event.target.checked ? formGroupName.controls['variable'].setValue(key) : formGroupName.controls['variable'].setValue()" [formControlName]="variable"/>