Mark Simpson Mark Simpson - 9 months ago 36
AngularJS Question

AngularJS - Model not updating on selection of radio button generated by ng-repeat

I am generating a bunch of radio buttons using ng-repeat, and then trying to update a model when one of them is selected. This doesn't appear to be working.

The same code markup works just fine when the radio inputs are hardcoded as opposed to being generated by ng-repeat.

This works:

<input type="radio" ng-model="lunch" value="chicken" name="lunch">
<input type="radio" ng-model="lunch" value="beef" name="lunch">
<input type="radio" ng-model="lunch" value="fish" name="lunch">

This doesn't:

<input type="radio" ng-model="lunch" ng-repeat="m in meat" value="m" name="lunch">

See jsfiddle showing both here:

Any help would be appreciated.


mpm mpm
<div ng-controller="DynamicCtrl">
    <input type="radio" ng-model="$parent.lunch" ng-repeat="m in meat"  
    ng-value="m" name="lunch">    

Should do the trick.

As I understand it, ng-repeat creates its own $scope. so you need to refer to the $parent $scope; Yes, AngularJS is tricky. Also you need to change the value to ng-value too.