javascript - 复选框 ng-model 值未根据复选框单击更改/更新

标签 javascript .net angularjs checkbox

我有以下复选框:-

<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/

相关文章:

javascript - .map() 将数组放入 renderRow() 内的 JSX 中

javascript - JavaScript 中的 64 位整数,来自 Mysql 请求

.net - 如何加速 "Verifying Application Requirements"窗口?

.net - 循环依赖与 DRY

javascript - Geolocation Cordova 插件导致应用卡住/崩溃

angularjs - 尝试使用 templateURL 测试 AngularJS 指令

javascript - 如何从 jquery ui datetimepicker 隐藏秒和毫秒

javascript - 如何控制(音频缓冲区)AudioContext() 的音量?

c# - 你什么时候以及为什么要向上转换一个对象?

javascript - AngularJS:访问AngularJS Controller 中的jquery变量