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

后端 未结 3 1276
北恋
北恋 2021-01-30 08:49

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 m

相关标签:
3条回答
  • 2021-01-30 09:07

    Just need to replace value with ng-value

    0 讨论(0)
  • 2021-01-30 09:17

    the above issue was discussed here

    That happens because ng-repeat creates a new scope. Basically, each <input> is creating a selectedOption value on its own inner scope. To work around that, create a new container object for that value. For example, you could declare in your controller:

    $scope.data = {selectedOption: x};
    

    And then in your template, use ng-model="data.selectedOption"

    in this way, ng-model gets updated .. :)

    this is tricky

    0 讨论(0)
  • 2021-01-30 09:32
    <div ng-controller="DynamicCtrl">
        <input type="radio" ng-model="$parent.lunch" ng-repeat="m in meat"  
        ng-value="m" name="lunch">    
        {{lunch}}
    </div>
    

    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.

    0 讨论(0)
提交回复
热议问题