简体   繁体   中英

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

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; ngFormControl for radio and select

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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