【问题标题】:Checkbox ng-model value not changing / updating as per checkbox click复选框 ng-model 值不会根据复选框单击更改/更新
【发布时间】:2019-05-07 06:40:22
【问题描述】:

我有下面的复选框:-

<input type="checkbox" ng-click="toggleShowOtherUserConfiguration()"
       ng-model="showOtherUserConfiguration" 
       name="ShowOtherUserConfiguration"
       value="showOtherUserConfiguration" />

在 app.controller 中我设置了:-

$scope.showOtherUserConfiguration = false;

因此,当页面加载时,该复选框将始终保持 false

但是发生了什么:-

页面加载时复选框处于未选中状态,此时$scope.showOtherUserConfiguration的值为false

当复选框被选中时,它的值仍然是false

当 Checkbox 状态从选中变为未选中时, $scope.showOtherUserConfiguration 值变为true

请指导我如何在未选中时保持false 和选中时保持true

注意:- 除了更改 $scope.showOtherUserConfiguration 值之外,不会更改 toggleShowOtherUserConfiguration() 函数中的任何内容

【问题讨论】:

    标签: javascript .net angularjs checkbox


    【解决方案1】:

    您不能将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>

    【讨论】:

      猜你喜欢
      • 2018-08-05
      • 1970-01-01
      • 2016-03-26
      • 2018-12-28
      • 2013-10-30
      • 1970-01-01
      • 2021-11-30
      • 2018-10-22
      • 1970-01-01
      相关资源
      最近更新 更多