如何将 radiobutton 的 checked 属性绑定(bind)到 Component 类的 boolean 变量?我想要的是,如果设置了 bool 标志,模板中的单选按钮将默认被选中。
我试过了 <input type="radio" [(ngModel)]="isSelected()">
但它抛出模板语法错误
platform-browser.umd.js:962EXCEPTION: Error: Uncaught (in promise): Template parse errors:
Parser Error: Unexpected token '=' at column 13 in [isSelected()=$event] in ChoiceComponent@5:6 ("
(change)="select()"
required
[ERROR ->][(ngModel)]="isSelected()"> {{choice.text}}</label>
</div>
"): ChoiceComponent@5:6
最佳答案
radio 还没有得到很好的支持。另见 Angular 2 forms; ngFormControl for radio and select
在解决这个问题之前,您可以尝试类似的方法
<input type="radio" [ngModel]="{selected: model.sex == 'male'}" (ngModelChange)="model.sex='male'" name="sex" value="male">Male<br>
关于javascript - Angular 2 如何将单选按钮的检查属性绑定(bind)到组件的 bool 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37561999/