javascript Angular JS 复选框 - 模型不默认为 false

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21363166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 20:44:09  来源:igfitidea点击:

Angular JS checkbox - model doesn't default to false

javascriptangularjsangularjs-scope

提问by jonhobbs

I have two checkboxes in my Angular JS form, like this...

我的 Angular JS 表单中有两个复选框,就像这样......

<input id="isBackground" type="checkbox" ng-model="addTemplate.data.isBackground">
<input id="repeats" type="checkbox" ng-model="addTemplate.data.repeats">

If I tick both boxes then write the $scope to the console, both values are set to true, but if I only tick one of them then one appears in the $scope (set to true) and the other one is simply missing. It should be in the $scope and set to 'false' surely?

如果我勾选两个框,然后将 $scope 写入控制台,则两个值都设置为 true,但如果我只勾选其中一个,则 $scope 中出现一个(设置为 true),而另一个则丢失。它应该在 $scope 中并确定设置为“false”吗?

采纳答案by Khanh TO

If a property assigned to ng-modelis undefined, ng-model will automatically create the property after the value is changed for the first time. It means from second time on, you will see both properties.

如果分配的属性ng-model不确定的,NG-模式将值变更为第一次后自动创建属性。这意味着从第二次开始,您将看到这两个属性。

DEMO

演示

To avoid this, you could set the default falsefor both properties:

为避免这种情况,您可以false为这两个属性设置默认值:

app.controller('MainCtrl', function($scope) {
  $scope.isBackground = false;
  $scope.repeats = false;
});

DEMO

演示

回答by Astronaut

A more flexible solution is to set a ng-init value, this works for dynamic proprieties as well as fixed, something that the accepted solution does not do.

一个更灵活的解决方案是设置一个 ng-init 值,这适用于动态属性和固定属性,这是公认的解决方案所不具备的。

<div class="checkbox">
    <label><input type="checkbox" value="" ng-init="formData[field.checkBox]=false" ng-model="formData[field.checkBox]">{{field.name}}</label>
</div>

回答by Satpal

You can use ngTrueValue, and ngFalseValue. I have use 1/0use it accordingly

您可以使用ngTrueValue, 和ngFalseValue。我已经1/0使用它相应地使用它

<input id="isBackground" ng-true-vale="1" ng-false-value="0" type="checkbox" ng-model="addTemplate.data.isBackground">
<input id="repeats" ng-true-vale="1" ng-false-value="0" type="checkbox" ng-model="addTemplate.data.repeats">

From Docs

来自文档

ngTrueValue: The value to which the expression should be set when selected.

ngFalseValue: The value to which the expression should be set when not selected.

ngTrueValue:选择时应将表达式设置为的值。

ngFalseValue:未选择时应将表达式设置为的值。

OR

或者

You can use ngChecked, If the expression is truthy, then special attribute "checked" will be set on the element

您可以使用ngChecked,如果表达式为真,则将在元素上设置特殊属性“checked”

<input id="isBackground" ng-checked="addTemplate.data.isBackground == true" type="checkbox" ng-model="addTemplate.data.isBackground">
<input id="repeats" ng-checked="addTemplate.data.repeats == true" type="checkbox" ng-model="addTemplate.data.repeats">