AngularJS - Binding radio buttons to models with boolean values
I am having a problem binding radio buttons to an object whose properties have boolean values. I am trying to display exam questions retrieved from a $resource.
HTML:
<label data-ng-repeat="choice in question.choices">
<input type="radio" name="response" data-ng-model="choice.isUserAnswer" value="true" />
{{choice.text}}
</label>
JS:
$scope.question = {
questionText: "This is a test question.",
choices: [{
id: 1,
text: "Choice 1",
isUserAnswer: false
}, {
id: 2,
text: "Choice 2",
isUserAnswer: true
}, {
id: 3,
text: "Choice 3",
isUserAnswer: false
}]
};
With this example object, the "isUserAnswer: true" property does not cause the radio button to be selected. If I encapsulate the boolean values in quotes, it works.
JS:
$scope.question = {
questionText: "This is a test question.",
choices: [{
id: 1,
text: "Choice 1",
isUserAnswer: "false"
}, {
id: 2,
text: "Choice 2",
isUserAnswer: "true"
}, {
id: 3,
text: "Choice 3",
isUserAnswer: "false"
}]
};
Unfortunately my REST service treats that property as a boolean and it will be difficult to change the JSON serialization to encapsulate those values in quotes. Is there another way to set up the model binding without changing the structure of my model?
Here's the jsFiddle showing non-working and working objects
The correct approach in Angularjs is to use ng-value
for non-string values of models.
Modify your code like this:
<label data-ng-repeat="choice in question.choices">
<input type="radio" name="response" data-ng-model="choice.isUserAnswer" data-ng-value="true" />
{{choice.text}}
</label>
Ref: Straight from the horse's mouth
That's an odd approach with isUserAnswer
. Are you really going to send all three choices back to the server where it will loop through each one checking for isUserAnswer == true
? If so, you can try this:
http://jsfiddle.net/hgxjv/4/
HTML:
<input type="radio" name="response" value="true" ng-click="setChoiceForQuestion(question1, choice)"/>
JavaScript:
$scope.setChoiceForQuestion = function (q, c) {
angular.forEach(q.choices, function (c) {
c.isUserAnswer = false;
});
c.isUserAnswer = true;
};
Alternatively, I'd recommend changing your tack:
http://jsfiddle.net/hgxjv/5/
<input type="radio" name="response" value="{{choice.id}}" ng-model="question1.userChoiceId"/>
That way you can just send {{question1.userChoiceId}}
back to the server.
<label class="rate-hit">
<input type="radio" ng-model="st.result" ng-value="true" ng-checked="st.result">
Hit
</label>
<label class="rate-miss">
<input type="radio" ng-model="st.result" ng-value="false" ng-checked="!st.result">
Miss
</label>
I tried changing value="true"
to ng-value="true"
, and it seems to work.
<input type="radio" name="response2" data-ng-model="choice.isUserAnswer" ng-value="true" />
Also, to get both inputs to work in your example, you'd have to give different name to each input -- e.g. response
should become response1
and response2
.