[英]Angular 7: Checkbox of type radio, use ngModel or change-function?
<label>Yes</label>
<input type="radio" name="dia?" [checked]="info.bDia" (change)="onchangeDia(dia?.checked)">
<label>No</label>
<input type="radio" name="dia?" [checked]="!info.bDia" (change)="onSaveDiabetes(dia?.checked)" />
I have an input of type of radio and two checkboxes.我有一个收音机类型的输入和两个复选框。 I wonder how to get the current value, because I need to change a variable.我想知道如何获取当前值,因为我需要更改一个变量。
onchangeDia(val: boolean){
this.info.bDia = val; // if I log both I get null
}
So, I have no clue how to do it.所以,我不知道该怎么做。 The problem is that jus one checkbox can be checked - I need to check which one.问题是只能选中一个复选框 - 我需要检查哪一个。 If the checkbox with the label "Yes" info.bDia
chould change to true.如果带有“是”标签的复选框,则info.bDia
应更改为 true。 I would do it with Two-Way-Data-Binding but it does not work for me neither.我会用双向数据绑定来做到这一点,但它对我也不起作用。
You don't need to add any function on change
.您不需要在change
上添加任何功能。 Only adding [(ngModel)]
will work.只有添加[(ngModel)]
有效。
Try like this:像这样尝试:
<label>Yes</label>
<input type="radio" name="dia" [value]="true" [(ngModel)]="info.bDia">
<label>No</label>
<input type="radio" name="dia" [value]="false" [(ngModel)]="info.bDia"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.