默认情况下已选中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
中设置了是非.
推荐答案
可以使用[(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
为true,则选择该收音机.如果它是错误的,那么另一个.
If rule.mode
is true, then that radio is selected. If it's false, then the other.
差异实际上归结于value
. 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屋!