2015-10-09 71 views
1

在以下代碼$scope.checkboxModel中設置爲false。當我點擊複選框並在第一次點擊時調試代碼時,當複選框被選中時,它將返回false而不是true。複選框在ng-click中返回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

因爲點擊就會觸發值,你會用NG-變化,而不是得到更好的服務下面的例子NG點擊獲取值在該事件處理程序中。 – kasia

+0

這是因爲您只在點擊複選框時調用showHidePassword方法。把手錶放在密碼上,然後調用這個方法。那麼它將工作,因爲你鍵入密碼字段 – CodingNinja

+0

@CodingNinja我不想用手錶 –

回答

1

由於版本不匹配而發生這種情況。在這一個看到,使用角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); 
 
    } 
 
});
<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>

相關問題