Radio button for boolean property

Philipp picture Philipp · Jun 29, 2016 · Viewed 49k times · Source

I have a simple boolean property valid in my object document and need to bind it to radio-inputs.

This is what i have so far:

<input type="radio" name="valid" id="validTrue" (click)="document.valid = true" [checked]="document.valid"/>
<input type="radio" name="valid" id="validFalse" (click)="document.valid = false" [checked]="!document.valid"/>

At least setting the property on click works but its state is no displayed by the radio-inputs. Looking in the developer console of my browser i found out that a property ng-reflect-checked is set but it doesnt seem to have impact on the html radio-input.

What am i doing wrong?
Does anyone have a working "angular2-boolean-radio-input" snippet?

Answer

G&#252;nter Z&#246;chbauer picture Günter Zöchbauer · Jun 29, 2016

In the new forms module this might do what you want

  <input type="radio" name="food" [(ngModel)]="document.valid" [value]="true">
  <input type="radio" name="food" [(ngModel)]="document.valid" [value]="false">

see also design doc for the new forms module