gpt4 book ai didi

javascript - 复选框 $viewValue 选中时显示为 'false'

转载 作者:行者123 更新时间:2023-11-30 12:23:12 26 4
gpt4 key购买 nike

我的复选框的默认值为“true”,我在其他地方以相同的形式使用复选框的 $viewValue。在应用程序开始时,复选框的 $viewValue 显示“false”,即使复选框被选中,但只有在用户关闭/打开复选框时才会显示。 What's going on here?

html

<body ng-app="myApp">
<form name="testForm">
<label>The Checkbox
<input type="checkbox" name="testCheck" data-ng-model="testCheck" data-ng-checked="true"></input>
</label>
</form>
<p>{{ testForm.testCheck.$viewValue }}</p>
</body>

javascript

angular.module('myApp', []);

最佳答案

不要将 ng-checkedng-model 一起使用,它们不能很好地协同工作。使用 ng-checked 只是在复选框上设置 checked 属性,它不会更新 ng-model 自动和检查属性的存在将搞乱初始模型更改。

使用 Controller 将模型初始化为 true。

例如:-

$scope.testCheck = true;

然后做:

<input type="checkbox" name="testCheck" data-ng-model="testCheck"></input>

你可以使用 ng-init从像这样的 View 中初始化模型:

<body ng-app="myApp" ng-init="testCheck = true">

虽然不适合使用ng-init如果 ng-repeat,用于初始化属性而不是别名特殊属性。

关于javascript - 复选框 $viewValue 选中时显示为 'false',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30353258/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com