簡體   English   中英

復選框ng-model值未根據復選框單擊更改/更新

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM