3
app.controller('radiusController', function($scope){
    $scope.radii = [
        {id:.25, checked:"false", name:"1/4 Mile"},
        {id:.5, checked:"false", name:"1/2 Mile"},
        {id:1, checked:"false", name:"1 Mile"},
        {id:2, checked:"true", name:"2 Mile"},
        {id:3, checked:"false", name:"3 Mile"},
        {id:4, checked:"false", name:"4 Mile"},
        {id:5, checked:"false", name:"5 Mile"}
    ];
    $scope.handleRadioClick = function(radius){
        window.radiochecked = radius.id;
    };
});

<li ng-repeat="radius in radii" id="selectradius-{{radius.id}}">

              <div class="radio">
                <label>

                  <input type="radio" name="radius"
                       ng-model="radius.checked"
                       ng-change="handleRadioClick(radius)">

                  {{radius.name}}

                </label>
              </div>

          </li>

How do I set the default radio button to be checked based on the checked value of scope radii? In this case the "2 Mile" should be checked by default.

plunker: http://plnkr.co/edit/RP9SpO1qGjn5Ua6pZJ3D?p=preview

1
  • set the value to ng-model variable so $scope.radius.checked = $scope.radii[0] Commented Aug 19, 2014 at 17:45

4 Answers 4

4

Simple. Eventhough the angular docs do not mention ng-checked, it is available.

<input type="radio" name="radius"
                       ng-change="handleRadioClick(radius)"
                       ng-checked="radius.checked">
Sign up to request clarification or add additional context in comments.

1 Comment

get Controller 'ngModel', required by directive 'ngChange', can't be found!
0

you can set the ng-model to the specified value you want in controller

$scope.radius.checked = $scope.radii[0]

4 Comments

Not sure what you mean. ng-model is currently set to invoke the ng-change function.
hgrathi, that didn't work. setting ng-model to a scope value does just that but not check the radio button.
Still haven't found a way to do this. Anyone else? See code above.
can you add the code in plunker. i would try to fix from there
0

All simple here

ngModel holds a model(where we want store selected item)
ngValue holds a value(what item related to this input)

To access scope which we want, we should step up from ngRepeat scope with $parent.

<div ng-repeat="radius in radii">
      <label>
        <input type="radio" name="radius"
             ng-model="$parent.model"
             ng-value="radius" />
        {{radius.name}}
      </label>
</div>  

Plunkr

Comments

0

Create an object to hold the selected value, like:

$scope.selectedValue = { id: 0 };

Update ng-model and value attributes like shown below:

 <input type="radio" name="radius" ng-change="handleRadioClick()" 
          ng-model="selectedValue.id" value="{{radius.id}}">

See working sample: http://plnkr.co/edit/zpNItMx13YPH0guvod0D?p=preview

1 Comment

It does not work. Just first click on each radio calls ngChange

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.