在下面的代码$scope.checkboxModel
中设置为false。当我在第一次单击时单击复选框并调试代码时,如果选中复选框,则返回false而不是true。
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.showPassword = '';
$scope.password = '';
$scope.checkboxModel = false;
$scope.showHidePassword = function(checkboxModel) {
$scope.showPassword = checkboxModel ? '' : $scope.password;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<div class="input-group bmargindiv1 col-md-12">
<span class="input-group-addon ndrftextwidth text-right" style="width:180px">Password :</span>
<input type="password" name="itemname" id="contactno" class="form-control" placeholder="password" ng-model="password">
</div>
<div class="input-group bmargindiv1 col-md-12">
<span class="input-group-addon ndrftextwidth text-right" style="width:180px">Show me Password <input type="checkbox" ng-model="checkboxModel" ng-click="showHidePassword(checkboxModel)" /></span>
<input type="text" name="itemname" id="contactno" class="form-control" placeholder="password" ng-model="showPassword" readonly="readonly">
</div>
</div>
为什么在选中复选框时第一次没有返回true?
答案 0 :(得分:1)
由于版本不匹配而发生这种情况。请参阅下面的示例,使用angular 1.3.14
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.showPassword = '';
$scope.password = '';
$scope.checkboxModel = false;
$scope.showHidePassword = function(checkboxModel) {
$scope.showPassword = checkboxModel ? $scope.password : '';
console.log("checkboxModel : " + checkboxModel);
}
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<div class="input-group bmargindiv1 col-md-12">
<span class="input-group-addon ndrftextwidth text-right" style="width:180px">Password :</span>
<input type="password" name="itemname" id="contactno" class="form-control" placeholder="password" ng-model="password">
</div>
<div class="input-group bmargindiv1 col-md-12">
<span class="input-group-addon ndrftextwidth text-right" style="width:180px">Show me Password <input type="checkbox" ng-model="checkboxModel" ng-click="showHidePassword(checkboxModel)" /></span>
<input type="text" name="itemname" id="contactno" class="form-control" placeholder="password" ng-model="showPassword" readonly="readonly">
</div>
</div>
&#13;