1

I'm having an object in my controller like the following:

$scope.colors = {
    green: "Green",
    yellow: "Yellow",
    red: "Red"
};

I'm trying to create the radio inputs dynamically and then bind the value of the input to the object's key.

I'm trying something like this:

<label ng-repeat="color in colors">{{color}}
<input type="radio" ng-model="model.color" name="name" ng-value="{{color}}" ng-change="test()" />
</label>

but I can't make it work.

Here is my fiddle

3 Answers 3

2

You never define your model on the controller. I have updated your fiddle to do so: https://jsfiddle.net/Xsk5X/1380/

  $scope.model = {"color":"test"};

I also added a <span> which displays the selected color to show it is working

I've added a new function and variable - $scope.createColors and $scope.colorsToBind.

The function will convert $scope.colors into an array of just the object keys, and then create a new array of Objects containing the key and value for that color, but as accessible fields; each will look like {key:"green", value: "Green"}. Once we have the array of these objects, the function will then set the value of $scope.colorsToBind to that array.

Your html is now using that new variable colorsToBind, and is displaying the value of each object but binding to the key of each one.

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

5 Comments

Imagine this colors object is fetched by a server. I want to be able to bind the keys of the object to the radio input's value and make a post request with them.
Ah I understand now. I've updated my answer with a new jsfiddle link, try it now and let me know if you need more explanation
In this way you also replace the labels of the radios with the key values. I want to keep using the labels as before.
I've updated the link to the jsfiddle again. Now you use the value for display but the key for binding.
Don't get me wrong but I was hoping in a cleaner more "angular" solution. For example before changing this form segment to radio buttons I had a select list. In the select list in angular you can do something like this <select ng-model="model.color" ng-change="sendColor()" ng-options="key as value for (key , value) in colors"></select> However if not a cleaner solution appears I'll mark yours as the answer. I appreciate your spent time.
1

I managed to come with a cleaner solution.

<label ng-repeat="(key, value) in colors">
    <input type="radio" ng-model="model.color" name="name" ng-value="key" /> {{value}}
</label>

here is the fiddle

Comments

0

you can do like this also :

<label ng-repeat="color in colors">{{color}}
<input type="radio" ng-model="colors" name="name" value="{{color}}" ng-change="test()" />
</label>

$scope.test = function() {
   alert($scope.colors);
};

Jsfiddle

7 Comments

This answer still submits the values of the object. I want to bind/submit the keys. e.g. bind yellow not "Yellow".
When someone picks the radio button with label Yellow I want to make a post request and send its key (yellow). The label and the value "Yellow" in the object is just a description.
ok @mosmic here I updated my fiddle, I added $scope.colorsName as an empty object, so when you post your data just pass this object so in ng-model it will send key of your color. may this will help you
jsfiddle.net/mosmic/Xsk5X/1383 I added a span to show you that the issue still remains
you added <span> outside ng-repeat so obviously you will not get color outside it
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.