mvermand
mvermand

Reputation: 6117

Boolean radio-buttons in Angular 2+ Forms

I want to be able to have a radio button group of two items in order to have a yes-no toggle.

The following snippet works, but has a "true"/"false" string value as item.value

<input type="radio" [formControl]="item" name="{{item.id}}" value=true>
<input type="radio" [formControl]="item" name="{{item.id}}" value=false>

How do I get an boolean value in item.value?

I tried the following without success:

<input type="radio" [formControl]="item" name="{{item.id}}" [ngValue]=true>
<input type="radio" [formControl]="item" name="{{item.id}}" [ngValue]=false>

It throws

Can't bind to 'ngValue' since it isn't a known native property

I am using Angular 2 RC4 with Angular Forms 0.2.0.

Upvotes: 8

Views: 9507

Answers (1)

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

Reputation: 657238

I think this is what you want:

<input type="radio" [formControl]="item" name="{{item.id}}" [value]=true>
<input type="radio" [formControl]="item" name="{{item.id}}" [value]=false>

Upvotes: 33

Related Questions