我有一个包含大约 100 个问题的表单,每个问题都有一个单选按钮和一些复选框,因此我需要用户能够保存该表单并稍后加载。我还需要检查用户在本次会话中更改了哪些内容。
这个问题解决了这个问题:如何表示 AngularJS 中哪些输入字段已更改 https://stackoverflow.com/questions/18641618/how-can-i-denote-which-input-fields-have-changed-in-angularjs
第二个答案(存储模型的旧值和当前值并比较两者)可以做到这一点。但是,如果我想使用 $pristine 解决方案,我就会遇到问题。取消选中 ng-checked 框不会改变它的 $pristine 值。只有在取消选中后再次选中该框,$pristine 值才会变为 false。
我知道我不应该将 ng-model 与 ng-check 一起使用,但我从服务器得到的答案为 1 或 0。用作模型的值不会检查复选框。
ng-model="question.answer" //will not check the box
ng-check="question.answer" //does check the box
来自服务器的值是 1。取消选中并选中该框会将其更改为“true”
<input ng-model="question.answer" ng-checked="question.answer"
type="checkbox" name="{{'answer' + question.id}}"/>
这是一个 plnkr:http://plnkr.co/edit/3xcI0Yq9WPZ1IxJJjKGt?p=preview http://plnkr.co/edit/3xcI0Yq9WPZ1IxJJjKGt?p=preview
你需要的是设置1
and 0
分别被视为真值和假值。您可以使用ng-true-value and ng-false-value https://docs.angularjs.org/api/ng/input/input%5Bcheckbox%5D进行设置。您不必处理将 1/0 转换为真/假,也可以摆脱ng-checked
并使用ng-model
本身有效。
<input ng-model="question.answer"
ng-true-value="1"
ng-false-value="0" type="checkbox" name="{{'answer' + question.id}}" />
Plnkr http://plnkr.co/edit/3YHzxm?p=preview
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)