使用AngularJs,我试图将输入字段的值存储到$scope.newPassword
,但只有在我键入至少6个字符后才能使用。{x>}它是默认行为还是有办法处理它?</ p>
这里有一些代码:
HTML
<input type="password" class="form-control" id="new-password" ng-model="newPassword" name="new-password" required pattern=".{6,}">
JS
app.controller('validationController', function($scope) {
$scope.noMatch = false;
$scope.newPassword="";
$scope.confirmNewPassword="";
$scope.passMatchTest = function(){
if($scope.newPassword!=$scope.confirmNewPassword){
$scope.noMatch = true;
$("#new-password, #confirm-new-password").css("background-color", "#f0b9b9");
}else{
$scope.noMatch = false;
$("#new-password, #confirm-new-password").css("background-color", "#aff3af");
}
};
$scope.$watch("newPassword", function(){
console.log($scope.newPassword);
$scope.passMatchTest();
});
$scope.$watch("confirmNewPassword", function(){
console.log($scope.confirmNewPassword);
$scope.passMatchTest();
});
});
console.log
显示未定义,除非超过6个字符。
答案 0 :(得分:2)
使用pattern
或ng-pattern
时,Angular会默认停止反映无效值。您可以添加ng-model-options="{allowInvalid: true}"
,这将允许将无效值填充到ng-model
。
<input type="password" class="form-control" id="new-password" ng-model="newPassword" name="new-password" required pattern=".{6,}" ng-model-options="{allowInvalid: true}">