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

921 views Asked by At

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>
1

There are 1 answers

3
vegas2033 On BEST ANSWER

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>