Angular 4 默认单选按钮默认选中 [英] Angular 4 default radio button checked by default
问题描述
我试图根据从对象中获得的值将单选按钮标记为默认值,它可以是 True 或 False.根据选项,我可以做些什么来标记为默认单选按钮?
I'm trying to mark as a default a radiobutton depending on the value I get from my object, it can be True or False. What could I do to mark as a default radiobutton depending on the option?
<label>This rule is true if:</label>
<label class="form-check-inline">
<input class="form-check-input" type="radio" name="mode" value="true"
[(ngModel)]="rule.mode"> all of the following conditions are true
</label>
<label class="form-check-inline">
<input class="form-check-input" type="radio" name="mode" value="false"
[(ngModel)]="rule.mode"> at least one of the following conditions is true
</label>
我在 rule.mode
中设置了真假.
I have the true or false set in rule.mode
.
推荐答案
您可以使用 [(ngModel)]
,但您需要将您的 value
更新为[value]
否则该值将作为字符串进行评估.它看起来像这样:
You can use [(ngModel)]
, but you'll need to update your value
to [value]
otherwise the value is evaluating as a string. It would look like this:
<label>This rule is true if:</label>
<label class="form-check-inline">
<input class="form-check-input" type="radio" name="mode" [value]="true" [(ngModel)]="rule.mode">
</label>
<label class="form-check-inline">
<input class="form-check-input" type="radio" name="mode" [value]="false" [(ngModel)]="rule.mode">
</label>
如果 rule.mode
为真,则选择该无线电.如果它是假的,那么另一个.
If rule.mode
is true, then that radio is selected. If it's false, then the other.
真正的区别在于值
.value="true"
实际计算结果为字符串 'true',而 [value]="true"
计算结果为布尔值 true.
The difference really comes down to the value
. value="true"
really evaluates to the string 'true', whereas [value]="true"
evaluates to the boolean true.
这篇关于Angular 4 默认单选按钮默认选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!