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].

3
  • stackoverflow.com/questions/31879497/… This might help Commented Nov 10, 2016 at 10:20
  • @Kapein Values are just strings in this example, I want to bind JSON objects to the value. Commented Nov 10, 2016 at 10:26
  • 1
    Any luck finding an answer here? I, too, am stuck with this one... Commented Nov 25, 2016 at 17:05

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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>

3 Comments

I don't want the value to be object.attribute, I want it to be the object itself.
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
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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.