我想覆盖 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:377 在http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js:18:314
提前感谢您的帮助!