我有以下复选框:-
<input type="checkbox" ng-click="toggleShowOtherUserConfiguration()"
ng-model="showOtherUserConfiguration"
name="ShowOtherUserConfiguration"
value="showOtherUserConfiguration" />
在 app.controller 中我设置了:-
$scope.showOtherUserConfiguration = false;
这样,当页面加载时,该复选框将始终保持false
。
但是发生的事情是:-
页面加载时,复选框保持未选中状态,此时,$scope.showOtherUserConfiguration
值为false
。
当复选框被选中时,其值保持false
。
当复选框状态从选中变为未选中时,
$scope.showOtherUserConfiguration
值变为 true
。
请指导我如何在未选中时保持 $scope.showOtherUserConfiguration
保持 false
并在选中时保持 true
?
注意:- 除了更改 $scope.showOtherUserConfiguration
值之外,不更改 toggleShowOtherUserConfiguration()
函数中的任何内容
最佳答案
您不能将 ng-click
与复选框一起使用,您需要将 ng-change
用于复选框事件。
试试这个:
<!DOCTYPE html>
<html ng-app="app">
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.18/angular.js"></script>
<script type="text/javascript">
var app = angular.module('app', []);
app.controller('OneController', function ($scope) {
$scope.showOtherUserConfiguration = false;
$scope.toggleShowOtherUserConfiguration = function (argument) {
console.log($scope.showOtherUserConfiguration);
}
});
</script>
</head>
<body ng-controller="OneController">
<input type="checkbox"
ng-change="toggleShowOtherUserConfiguration()"
ng-model="showOtherUserConfiguration"
name="ShowOtherUserConfiguration"
value="showOtherUserConfiguration" />
{{showOtherUserConfiguration}}
</body>
</html>
关于javascript - 复选框 ng-model 值未根据复选框单击更改/更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56016870/