2017-05-04 315 views
7

我试图将标记为默认的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”

+2

[attr.checked] = “role.mode” –

+0

@BharatChauhan这是一个正确的答案,完美的 '初始化只' 设置的值。谢谢! – CularBytes

回答

12

您可以使用[(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是真的,那么无线电选择。如果它是错误的,那么另一个。

差异真的归结为valuevalue="true"的计算结果为字符串'true',而[value]="true"的计算结果为布尔值true。

+0

谢谢你,我的日子!^^ – XGuy

+0

谢谢,你救了我的一天! – Buminda

-1

我认为这应该工作:

<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>