1

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
Tuomas Toivonen
  • 21,690
  • 47
  • 129
  • 225
  • 1
    `[(ngModel)]="isSelected()"` is invalid. You can't two-way bind to a function. What is the intention of your code? How does `isSelected()` look like? – Günter Zöchbauer Jun 01 '16 at 07:27

1 Answers1

1

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

Community
  • 1
  • 1
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567