6

我想覆盖 AngularJS 的电子邮件验证器。我希望它使用我的自定义字符串来验证电子邮件地址。我在他们的文档中找到的代码是这样的:JS:

   var app = angular.module('registrationApp', []);

    app.directive('overwriteEmail', function() {
      var EMAIL_REGEXP = /^[a-z0-9!#$%&'*+/=?^_`{|}~.-]+@example\.com$/i;

      return {
        require: 'ngModel',
        restrict: '',
        link: function(scope, elm, attrs, ctrl) {
            if (ctrl && ctrl.$validators.email) {

                // this will overwrite the default Angular email validator
                ctrl.$validators.email = function(modelValue) {
                    return ctrl.$isEmpty(modelValue) || EMAIL_REGEXP.test(modelValue);
                }
            }
        }
    }
});

HTML:

<div data-ng-app="registrationApp" data-ng-init="">
  <form name="form" class="css-form" novalidate>
  <div>
      Overwritten Email:
      <input type="email" ng-model="myEmail" overwrite-email name="overwrittenEmail" />
      <span ng-show="form.overwrittenEmail.$error.email">This email format is invalid!     </span><br>
      Model: {{myEmail}}
    </div>
  </form>
</div>

但是代码不起作用。抛出的错误是:

TypeError:无法在 N (http://ajax.googleapis.com/ajax/libs/的链接( http://localhost:63342/goga_tests/js/registration.js:39:32 )处读取未定义的属性“电子邮件” angularjs/1.2.26/angular.min.js:54:372 ) 在 g ( http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js:47:256 ) 在g ( http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js:47:273 ) 在 N ( http://ajax.googleapis.com/ajax/libs/angularjs /1.2.26/angular.min.js:54:313 ) 在 g ( http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js:47:256 ) 在 N ( http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js:54:313) 在 g ( http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js:47:256 ) 在http://ajax.googleapis.com/ajax/libs/angularjs /1.2.26/angular.min.js:46:377http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js:18:314

提前感谢您的帮助!

4

1 回答 1

12

您使用的角度版本1.2.26不会$validators在 ngModel 控制器实例上添加特殊属性。您需要 1.3.x 版本的 angular。

如果您有 1.3.x,您可以按原样使用您的指令,并且您可以添加2大于 ng-model(即 1)的优先级,以便您的指令在ng-model指令轮到它之前设置验证器:

app.directive('overwriteEmail', function() {
  var EMAIL_REGEXP = /^[a-z0-9!#$%&'*+/=?^_`{|}~.-]+@example\.com$/i;

  return {
    require: 'ngModel',
    restrict: '',
    priority: 2,
    link: function(scope, elm, attrs, ctrl) {
      if (ctrl && ctrl.$validators.email) {
        // this will overwrite the default Angular email validator
        ctrl.$validators.email = function(modelValue) {
          return ctrl.$isEmpty(modelValue) || EMAIL_REGEXP.test(modelValue);
        }
      }
    }
  }
});

演示

var app = angular.module('app', []);

app.directive('overwriteEmail', function() {
  var EMAIL_REGEXP = /^[a-z0-9!#$%&'*+/=?^_`{|}~.-]+@example\.com$/i;

  return {
    require: 'ngModel',
    restrict: '',
    priority: 1,
    link: function(scope, elm, attrs, ctrl) {
      if (ctrl && ctrl.$validators.email) {
        // this will overwrite the default Angular email validator
        ctrl.$validators.email = function(modelValue) {
          return ctrl.$isEmpty(modelValue) || EMAIL_REGEXP.test(modelValue);
        }
      }
    }
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js"></script>
<div ng-app="app">
  <div>
    <form name="form" class="css-form" novalidate>
      <div>
        Overwritten Email:
        <input type="email" ng-model="myEmail" overwrite-email name="overwrittenEmail" />
        <span ng-show="form.overwrittenEmail.$error.email">This email format is invalid!     </span>
        <br>Model: {{myEmail}}
      </div>
    </form>
  </div>
</div>

如果模式无效,您也可以坚持使用ng-pattern和使用来显示消息。form.overwrittenEmail.$error.pattern

<input type="email" ng-pattern="/^[a-z0-9!#$%&'*+/=?^_`{|}~.-]+@example\.com$/i" 
                    ng-model="myEmail" name="overwrittenEmail" />
   <span ng-show="form.overwrittenEmail.$error.pattern">

演示

var app = angular.module('app', []);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
  <div>
    <form name="form" class="css-form" novalidate>
      <div>
        Overwritten Email:
        <input type="email" ng-model="myEmail" ng-pattern="/^[a-z0-9!#$%&'*+/=?^_`{|}~.-]+@example\.com$/i" name="overwrittenEmail" />
        <span ng-show="form.overwrittenEmail.$error.pattern">This email format is invalid!     </span>
        <br>Model: {{myEmail}}
      </div>
    </form>
  </div>
</div>

于 2015-01-14T21:22:56.990 回答