2

Is it possible to bind a JSON object to a radio input in Angular2?

I have a JSON object named object and I tried

<input type="radio" [(ngModel)]="selectedObject" [ngValue]="object">

but it gives ngValue is not a known property of input error. I also tried

<input type="radio" [(ngModel)]="selectedObject" value="{{object}}">

But then selectedObject becomes [object Object].

Bünyamin Sarıgül
  • 3,031
  • 4
  • 30
  • 55

2 Answers2

1

Thanks to this post, we have an answer:
Use [value]

<label *ngFor="let item of items">
    <input type="radio" formControlName="options" [value]="item">
    {{item}}
</label>

Thanks to Colleen Purcell

Jon
  • 7,848
  • 1
  • 40
  • 41
0

I wrote this code in Angular 1, without testing converted to angular 2 for you

<span *ngFor="let List in object.Lists">
    <input type="radio" name="{{List.ID}}" value="{{List.Value}}">
</span>
Kapein
  • 175
  • 13
  • I don't want the value to be `object.attribute`, I want it to be the `object` itself. – Bünyamin Sarıgül Nov 10 '16 at 10:37
  • and what do you think about using indexes and then OnChange(index), then you look up which index in the array you want and copy it? Not sure if that works with [(ngModel)], not an Angular 2 expert – Kapein Nov 10 '16 at 10:40
  • If I use `onChange`, the model is updated when I select the button but it doesn't be selected when the model is changed externally (not two way binding). – Bünyamin Sarıgül Nov 10 '16 at 10:51