0

I have some data coming back from a resource that looks like:

$scope.phones = [
    {
        Value: <some value>,
        IsDefault: true
    },
    {
        Value: <some value>
        IsDefault: false
    }
];

And for simplicity sake, here's the repeater:

<div ng-repeat="phone in phones">
    <input type="radio" name="phone" ng-model="phone.IsDefault" />
</div>

I would like whichever radio is checked to update the model accordingly - this is not happening. On page load, nothing is checked. I can use ng-checked - but without ng-model it wont bind back to the array. Am I missing something simple or am I stuck writing an ng-change event to manually update the array?

As of now, I wrote a ng-change event as well, it currently looks like:

ng-model="phone.IsDefault" ng-value="true" ng-change="newPhoneSelected($index)"

$scope.newPhoneSelected = function (index) {
    for (var i = 0; i < $scope.phones.length; i++) {
        if (i == index) $scope.phones[i].IsDefault = true;
        else $scope.phones[i].IsDefault = false;
    }
}
2
  • ng-value... it is radio button they work based on the ng-value Commented May 22, 2014 at 21:51
  • Have tried using ng-true-value="true" and ng-false-value="false" - no luck - binding does not happen Commented May 22, 2014 at 21:52

2 Answers 2

2

You are missingng-value... it is radio button they work based on value to mark a value as selected. You need either [value="" | ng-value=""]

<input type="radio"
       ng-model=""
       [value="" |
       ng-value=""]>

Like:

 <input type="radio" ng-value="true" name="boolean" ng-model="myValue" /> True
 <input type="radio" ng-value="false" name="boolean"  ng-model="myValue" /> False 

Here is a plunker demo

Or with strings values:

$scope.myValue = 'Car'

html

 <input type="radio" ng-value="'Car'" name="string1" ng-model="myValue" /> Car
 <input type="radio" ng-value="'Airplane'" name="string1"  ng-model="myValue" /> Airplane 

Here is the second demo

This is probably the closest sample to what you have:

http://jsfiddle.net/JbMuD/

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

3 Comments

ng-false-value="false" ng-true-value="true" ng-model="phone.IsDefault" - No change.
My radios are within a repeater tho - so setting one value to false isn't an option
@tymeJV you are missing the point ng-model="myValue" let's say 'Car' only will mark selected the radio button with value="Car"
0

A radio button is used to select one out of many values. You want to change the property of one item (isDefault = true) and simultaneously of another one (isDefault = false).

The semantically correct way would be to have some kind of defaultPhone value:

<div ng-repeat="phone in phones">
  <input type="radio" name="phone" ng-model="temp.defaultPhone" ng-value="phone"/>
</div>

Since your model requires that each phone "knows" itself wether it's default or not, you can add a listener:

$scope.$watch('temp.defaultPhone', function(defaultPhone) {
  $scope.phones.forEach(function(phone) {
    phone.isDefault = phone === defaultPhone;
  });
});  

Here's a working example.

Comments

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.