Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了AngularJS – 绑定单选按钮到具有布尔值的模型大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个问题绑定单选按钮到一个对象的属性具有布尔值。我试显示从$资源检索的试问题。

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.",isUserAnswer: "false"
        },isUserAnswer: "true"
        },isUserAnswer: "false"
        }]
};

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

Here’s the jsFiddle showing non-working and working objects

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

Straight from the horse’s mouth

大佬总结

以上是大佬教程为你收集整理的AngularJS – 绑定单选按钮到具有布尔值的模型全部内容,希望文章能够帮你解决AngularJS – 绑定单选按钮到具有布尔值的模型所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。