【问题标题】:AngularJs- Form validation directive. The password confirm is always invalidAngularJs- 表单验证指令。密码确认始终无效
【发布时间】:2017-04-06 12:39:19
【问题描述】:

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

<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.log ng-show 条件;当密码匹配时,条件变为未定义。

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

分开;首先.required 变为未定义然后.noMatch

【问题讨论】:

    标签: angularjs validation ionic-framework angularjs-directive


    【解决方案1】:

    我现在无法测试它,但我相信 $parsers 期望您返回一个值,除非您遇到解析器错误。
    尝试在您的代码中添加return

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

    【讨论】:

    • 只是想知道,为什么在纯 AngularJs 应用程序中不需要返回并且可以工作?就像在这个例子中:jsfiddle.net/thomporter/UZrex/1
    猜你喜欢
    • 2017-12-06
    • 2014-10-18
    • 2017-12-19
    • 1970-01-01
    • 2018-09-26
    • 1970-01-01
    • 1970-01-01
    • 2021-02-12
    • 1970-01-01
    相关资源
    最近更新 更多