Tuomas Toivonen
Tuomas Toivonen

Reputation: 23492

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

Upvotes: 1

Views: 1104

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657238

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

Upvotes: 1

Related Questions