简体   繁体   中英

Angular - using radio buttons

I believe I'm following correctly the AngularJS official documentation for radio buttons .

So, I created this code:

indexSelected={{indexSelected}}
<form name="form">
    <div class="form-group">
        <label ng-repeat="s in prices track by $index" style="width: 100%">
            <input type="radio" name ="option" ng-model="indexSelected" value="{{s.months}}"> {{s.price}} + vat
        </label>
    </div>
</form>

Also, I made other attempting using ng-value, like this...

indexSelected={{indexSelected}}
<form name="form">
    <div class="form-group">
        <label ng-repeat="s in prices track by $index" style="width: 100%">
            <input type="radio" name ="option" ng-model="indexSelected" ng-value="s.months"> {{s.price}} + vat
        </label>
    </div>
</form>

and this is my controller

angular
.module('app')
.controller('ModalInstanceUpgradeSolutionCtrl', function ($scope,$rootScope, $uibModalInstance, appId) {
    $scope.prices = [{
        months: 1, price : 20
        }, { months 2: price: 40}]
     });

The question is: What could be wrong ? because when I click on the radio buitton, this is not updating the model indexSelected. Any clue ?

Ok, found the error. @charlietfl gave me some hint. Enough to google it.

If I replace ng-model="indexSelected" for ng-model="$parent.indexSelected", this will access the parent scope. ng-repeat creates a child scope.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM