[英]Checkbox ng-model value not changing / updating as per checkbox click
我有以下復選框:-
<input type="checkbox" ng-click="toggleShowOtherUserConfiguration()"
ng-model="showOtherUserConfiguration"
name="ShowOtherUserConfiguration"
value="showOtherUserConfiguration" />
在app.controller中我已經設置了:-
$scope.showOtherUserConfiguration = false;
因此,在加載頁面時,該復選框將始終為false
。
但是發生了什么事:
加載頁面時,復選框保持未選中狀態,此時$scope.showOtherUserConfiguration
值為false
。
選中復選框時,其值保持為false
。
當Checkbox狀態從“ checked”更改為“ unchecked”時, $scope.showOtherUserConfiguration
值變為true
。
請指導我$scope.showOtherUserConfiguration
如何在未選中的$scope.showOtherUserConfiguration
保持false
在選中的$scope.showOtherUserConfiguration
保持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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.