简体   繁体   中英

AngularJS : binding radio buttons to ng-model with angular in ng-repeat

I'm currently learning angular.js and I'm running into a problem. I'm listing radio buttons based on the items fetched from the database and when I click on any of them my model doesn't get updated. I added an input to test and the input update the model right away.

Any idea what i'm missing?

Update: it appears that the problem comes from bootstrap and the span wrapping the radio buttons. If I remove the spans the model gets updated

<div ng-repeat="question in questionnaire">
  <div class="btn-group col-xs-offset-1 col-xs-2 col-sm-2" data-toggle="buttons">
    <span class="btn btn-primary">
      <input type="radio" name="question{{$index}}" ng-model="formData[$index]" ng-value="false"> No
    </span>
    <span class="btn btn-primary">
      <input type="radio" name="question{{$index}}" ng-model="formData[$index]" ng-value="true"> Yes
    </span>
    <input type="text" name="testEntry{{$index}}" ng-model="formData[$index]" />
  </div>
  {{formData}}
  <span ng-bind="question.QuestionPhrase" class="message col-xs-8 col-sm-8"></span>
</div>

So it turns out that the problem was coming from the spans. I changed them to labels and it worked.

Here is the code:

<div ng-repeat="question in questionnaire">
                <div class="btn-group col-xs-offset-1 col-xs-2 col-sm-2" data-toggle="buttons">
                    <label class="btn btn-primary">
                        <input type="radio" name="question{{$index}}" ng-model="formData[$index]" value="false"> No
                    </label>
                    <label class="btn btn-primary">
                        <input type="radio" name="question{{$index}}" ng-model="formData[$index]" value="true"> Yes
                    </label>
                </div>
                {{formData}}
                <span ng-bind="question.QuestionPhrase" class="message col-xs-8 col-sm-8"></span>
                <br /><br /><br />
            </div>

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