AngularJs-表单验证指令。密码确认始终无效

问题描述:

我在我的离子应用程序(the code available here)中有注册表单,注册视图有它自己的控制器。密码确认有一个指令来检查与主密码的匹配。注册按钮被禁用,直到表单有效。但该表格永远不会生效password_c输入总是无效AngularJs-表单验证指令。密码确认始终无效

<ion-view view-title="Register Form"> 
    <ion-content> 
    <form ng-submit="signup()" name="regForm" novalidate> 
     <div> 
     <label for="email" .....></label> 
     <label for="password" ......></label> 
     <label for="password_c"> 
       <input type="password" id="password_c" name="password_c" ng-model="registerForm.password_c" valid-password-c required> 
        <span ng-show="regForm.password_c.$error.required && regForm.password_c.$dirty">Please confirm your password.</span> 
        <span ng-show="!regForm.password_c.$error.required && regForm.password_c.$error.noMatch && regForm.password.$dirty">Passwords do not match.</span> 
      </label> 
       <button type="submit" ng-disabled="!regForm.$valid"> 
     </div> 
    </form> 
    </ion-content> 
</ion-view> 

,这是指令:

.directive('validPasswordC', function() { 
     return { 
     require: 'ngModel', 
     link: function (scope, elm, attrs, ctrl) { 
      ctrl.$parsers.unshift(function (viewValue, $scope) { 
      var noMatch = viewValue != scope.regForm.password.$viewValue 
      ctrl.$setValidity('noMatch', !noMatch) 
      }) 
     } 
     } 
    }) 

我做console.logng-show条件;当密码匹配时,条件变得不明确。

console.log(!regForm.password_c.$error.required && regForm.password_c.$error.noMatch && regForm.password.$dirty) 

另外;首先.required变得不确定,然后.noMatch

我现在不能测试它,但我相信$parsers期望您除非有解析器错误才会返回一个值。
试着在你的代码中添加return

ctrl.$parsers.unshift(function (viewValue) { 
    var noMatch = viewValue != scope.regForm.password.$viewValue; 
    ctrl.$setValidity('noMatch', !noMatch); 
    return viewValue; 
}); 
+0

你救了我的命:)它的工作 –

+0

只知道,为什么在纯AngularJs Apps是需要和工作不归路?像这个例子一样:http://jsfiddle.net/thomporter/UZrex/1/ –