【问题标题】:AngularJS : $pristine for ng-check checked inputsAngularJS:用于 ng-check 检查输入的 $pristine
【发布时间】:2014-10-29 00:07:15
【问题描述】:

我有一个包含大约 100 个问题的表单,每个问题都有一个单选框和一些复选框,因此我需要用户能够保存表单并稍后加载它。我还需要检查用户在此会话中更改了哪些内容。

本题解题: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

【问题讨论】:

    标签: angularjs angularjs-ng-model


    【解决方案1】:

    您需要将1 和0 分别设置为true 和false 值。您可以使用ng-true-value and ng-false-value 进行设置。您不必处理将 1/0 转换为 true/false,也可以摆脱 ng-checked 并有效地使用 ng-model 本身。

    <input ng-model="question.answer" 
                ng-true-value="1" 
                ng-false-value="0" type="checkbox" name="{{'answer' + question.id}}" />
    

    Plnkr

    【讨论】:

    • 做到了,谢谢。摆脱了 ng-checked 并且 $pristine 工作正常
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-27
    • 1970-01-01
    • 2018-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多