angularjs指令验证发送前的唯一电子邮件检查 html
那么有没有办法检查这封邮件是否有效 在将值发送到服务器之前 更新 在确定运行了其他验证程序之后,在这里使用push()将其作为最后一个解析器运行 因此,只有在通过要求和电子邮件验证的情况下 打电话检查唯一的电子邮件 最终 具有编译和优先级 如果scope.form.email的值为$isvalid,它仍然不起作用angularjs指令验证发送前的唯一电子邮件检查 html,angularjs,angularjs-directive,Angularjs,Angularjs Directive,那么有没有办法检查这封邮件是否有效 在将值发送到服务器之前 更新 在确定运行了其他验证程序之后,在这里使用push()将其作为最后一个解析器运行 因此,只有在通过要求和电子邮件验证的情况下 打电话检查唯一的电子邮件 最终 具有编译和优先级 如果scope.form.email的值为$isvalid,它仍然不起作用 无法使用且似乎已过时:(解析器的顺序很重要。我不确定,但您的解析器可能已在内置解析器之前注册,因此首先被触发 也许如果你拼接$parsers数组并在0插入你的解析器,它可能会工作。谢谢
无法使用且似乎已过时:(解析器的顺序很重要。我不确定,但您的解析器可能已在内置解析器之前注册,因此首先被触发
也许如果你拼接
$parsers
数组并在0
插入你的解析器,它可能会工作。谢谢你的观点,我的目标是在要求和电子邮件传递后验证唯一的电子邮件(我为此推送)。奇怪的是这个控制台。log(ngModelCtrl.$parsers)给我[]而不是至少ng require和ng email…好的,原因可能是指令优先级。查看指令的优先级文档,为您的指令设置适当的优先级。我已经更新了con优先级并编译(但我认为优先级也适用于链接块)我认为$parsers的实现目前是同步的,因此它不会等待承诺。您的结果可能会很奇怪。您所能做的最好是验证它看起来像电子邮件。请参阅我提供的答案中的注释。这可能是优先问题。
<input type="email" data-input-feedback="" data-ng-model="user.email" data-unique-email="" required="required" placeholder="Email" class="form-control" id="email" name="email">
.directive('uniqueEmail',function (User) {
return {
require:'ngModel',
restrict:'A',
link:function (scope, element, attrs, ngModelCtrl) {
ngModelCtrl.$parsers.push(function (viewValue) {
/*
Is there a way to check if it's a valid email ?
both ngModelCtrl.$valid and ngModelCtrl.$error.email doesn't work
*/
User.isUniqueEmail(viewValue).then(function(data){
ngModelCtrl.$setValidity('uniqueEmail', !data.email);
});
return viewValue;
});
}
};
});
ngModelCtrl.$parsers.push
.directive('uniqueEmail',function (User) {
return {
require:'ngModel',
restrict:'A',
controller:function ($scope) {
$scope.isValidEmail = function(){
return $scope.form.email.$error.email;
}
},
link:function (scope, element, attrs, ngModelCtrl) {
var original;
// If the model changes, store this since we assume it is the current value of the user's email
// and we don't want to check the server if the user re-enters their original email
ngModelCtrl.$formatters.unshift(function(modelValue) {
original = modelValue;
return modelValue;
});
// using push() here to run it as the last parser, after we are sure that other validators were run
ngModelCtrl.$parsers.push(function (viewValue) {
if (viewValue && viewValue !== original ) {
if(scope.isValidEmail(viewValue)){
User.isUniqueEmail(viewValue).then(function(data){
ngModelCtrl.$setValidity('uniqueEmail', !data.email);
});
}
return viewValue;
}
});
}
};
});
.directive('uniqueEmail',function (User) {
return {
require:'ngModel',
restrict:'A',
priority:0,
compile: function compile(tElement, tAttrs, transclude) {
return function (scope, element, attrs, ngModelCtrl) {
var original;
// If the model changes, store this since we assume it is the current value of the user's email
// and we don't want to check the server if the user re-enters their original email
ngModelCtrl.$formatters.unshift(function(modelValue) {
original = modelValue;
ngModelCtrl.$setViewValue(original);
return modelValue;
});
// using push() here to run it as the last parser, after we are sure that other validators were run
ngModelCtrl.$parsers.push(function (viewValue) {
if (viewValue && viewValue !== original ) {
if(scope.isValidEmail()){
User.isUniqueEmail(viewValue).then(function(data){
ngModelCtrl.$setValidity('uniqueEmail', !data.email);
});
}
return viewValue;
}
});
scope.isValidEmail = function(){
return scope.form.email.$isvalid;
}
}
}
}
});