简体   繁体   English

Angular 2 如何将单选按钮的检查属性绑定到组件的布尔属性?

[英]Angular 2 how to bind check property of radiobutton to boolean attribute of component?

How do I bind the checked property of radiobutton to boolean variable of Component class?如何将radiobutton 的checked 属性绑定到Component 类的布尔变量? What I desire, that radiobutton in template will be checked by default if the boolean flag is set.我想要的是,如果设置了布尔标志,默认情况下将检查模板中的单选按钮。

I tried <input type="radio" [(ngModel)]="isSelected()"> but it throws template syntax error我试过<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 isn't well supported yet.收音机还没有得到很好的支持。 See also Angular 2 forms;另见Angular 2 形式; ngFormControl for radio and select ngFormControl 用于收音机和选择

Until this is fixed you can try something like在解决此问题之前,您可以尝试类似的操作

<input type="radio" [ngModel]="{selected: model.sex == 'male'}" (ngModelChange)="model.sex='male'"  name="sex" value="male">Male<br>

See also How to bind to radio buttons in angular2 beta 6另请参阅如何绑定到 angular2 beta 6 中的单选按钮

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM