203

我在将单选按钮绑定到其属性具有布尔值的对象时遇到问题。我正在尝试显示从 $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
        }]
};   

对于此示例对象,“isUserAnswer: true”属性不会导致选择单选按钮。如果我将布尔值封装在引号中,它就可以工作。

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"
        }]
};   

不幸的是,我的 REST 服务将该属性视为布尔值,并且很难更改 JSON 序列化以将这些值封装在引号中。是否有另一种方法可以在不更改模型结构的情况下设置模型绑定?

这是显示非工作和工作对象的 jsFiddle

4

7 回答 7

388

Angularjs 中的正确方法是ng-value用于模型的非字符串值。

像这样修改你的代码:

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

参考:直接从马嘴里

于 2013-08-26T14:32:35.127 回答
21

这是一个奇怪的方法isUserAnswer。您真的要将所有三个选项都发送回服务器,它会循环检查每个选项isUserAnswer == true吗?如果是这样,你可以试试这个:

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;
};

或者,我建议改变你的策略:

http://jsfiddle.net/hgxjv/5/

<input type="radio" name="response" value="{{choice.id}}" ng-model="question1.userChoiceId"/>

这样你就可以发送{{question1.userChoiceId}}回服务器。

于 2013-06-06T19:38:55.207 回答
12
 <label class="rate-hit">
     <input type="radio" ng-model="st.result" ng-value="true" ng-checked="st.result">
     Hit
 </label>
 &nbsp;&nbsp;
 <label class="rate-miss">
     <input type="radio" ng-model="st.result" ng-value="false" ng-checked="!st.result">
     Miss
 </label>
于 2015-05-06T09:43:57.813 回答
9

我尝试更改value="true"ng-value="true",它似乎有效。

<input type="radio" name="response2" data-ng-model="choice.isUserAnswer" ng-value="true" />

此外,要让两个输入都在您的示例中工作,您必须为每个输入指定不同的名称 - 例如response应该成为response1and response2

于 2013-08-14T20:51:12.420 回答
1

你可以看看这个:

https://github.com/michaelmoussa/ng-boolean-radio/

这个人写了一个自定义指令来解决“true”和“false”是字符串而不是布尔值的问题。

于 2013-06-06T20:14:56.563 回答
0

如果您决定引用所有真实值,您的收音机在小提琴中设置的方式 - 共享相同的模型 - 将仅导致最后一组显示选中的收音机。更可靠的方法是为各个组提供自己的模型,并将值设置为无线电的唯一属性,例如 id:

$scope.radioMod = 1;
$scope.radioMod2 = 2;

这是新 html 的表示:

<label data-ng-repeat="choice2 in question2.choices">
            <input type="radio" name="response2" data-ng-model="radioMod2" value="{{choice2.id}}"/>
                {{choice2.text}}
        </label>

还有一把小提琴。

于 2013-06-06T19:41:27.623 回答
0

如果您使用布尔变量来绑定单选按钮。请参考下面的示例代码

<div ng-repeat="book in books"> 
<input type="radio" ng-checked="book.selected"  
ng-click="function($event)">                        
</div>
于 2018-01-11T07:36:16.477 回答