Angular 4默认radioButton默认设置
问题描述:
我试图将标记为默认的radiobutton取决于我从我的对象获得的值,可以是True或False。 如何根据选项标记为默认单选按钮?Angular 4默认radioButton默认设置
<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 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]
否则该值评估为字符串。它应该是这样的:
<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
是真的,那么无线电选择。如果它是错误的,那么另一个。
差异真的归结为value
。 value="true"
的计算结果为字符串'true',而[value]="true"
的计算结果为布尔值true。
答
我认为这应该工作:
<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>
[attr.checked] = “role.mode” –
@BharatChauhan这是一个正确的答案,完美的 '初始化只' 设置的值。谢谢! – CularBytes