gpt4 book ai didi

javascript - angularjs 1.2 中的异步验证

转载 作者:行者123 更新时间:2023-11-28 19:25:53 25 4
gpt4 key购买 nike

我正在尝试创建一个电子邮件存在验证指令,该指令会调用 ajax 来查看电子邮件是否存在。我无法使用此代码更新该字段的验证。

app.directive('uniqueValidator', ['UniqueValidatorService',function(uniqueValidatorService) 
{
var linkFn=function(scope, elem, attrs,ngModel)
{
var vm=this;
vm.ngModel=ngModel;
vm.scope=scope;
var config=scope.$eval(attrs.uniqueValidator);
console.log('Value of the atribute'+window.angular.toJson(config));
/*If the value is truly unique then we would want to set validation as passed*/
vm.successCallback=function successCallback(unique){
vm.ngModel.$setValidity('unique',false);
console.log('Value of validity success case'+vm.ngModel.$valid);
};
/*Set validity as false to make sure we have a */
vm.errorCallback=function errorCallback(error){
ngModel.$setValidity('unique',true);
console.log('Value of validity'+ngModel.$valid);
};
var blurHandler=function(event){
console.log('In blur'+ngModel);
if (!ngModel || !elem.val()) return;
var currentValue=elem.val();
uniqueValidatorService.checkUniqueness(config.url,config.property,currentValue).then(function(data){
vm.successCallback(data);

},function(error){vm.errorCallback(error)});
}
elem.bind('blur',blurHandler.bind(vm));

};
return {
restrict: 'A',
scope:{},
require:'ngModel',
link: linkFn
};
}
])
.directive('uniqueErrorMessage',function(){
var linkFn= function(scope, elem, attrs,ngModel){
scope.message='Email is in use';
console.dir(scope.registrationForm.email.$error);
};
return {
restrict: 'E',
replace:true,
template:'<span ng-show="registrationForm.email.$dirty && registrationForm.email.$error.unique" >{{message}}</span>',
link: linkFn
};

});

独特的错误消息指令没有根据验证结果正确更新。即使验证通过,它仍然显示在 DOM 上。另外,我还看到即使验证失败,输入元素的 ngModel.$valid 也会变为 true。我尝试将 $setValidity 封装在范围内。$apply 但这会导致摘要进行中错误。对此有任何帮助感谢您的支持。我将无法包含 ui 路由器验证或升级我的 angularjs 版本以使用较新的验证框架

谢谢,拉胡尔

最佳答案

下面是一个可能有帮助的示例:

var app = angular.module('app', []);
app.controller('ctrl', function() {});

app.directive('unique', function(userFactory) {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, element, attr, ngModelController) {
element.on('blur', function() {
if (ngModelController.$viewValue != '') {
userFactory.tryUsername(ngModelController.$viewValue)
.then(
function succcess(result) {
ngModelController.$setValidity('unique', true);
},
function fail(result) {
ngModelController.$setValidity('unique', false);
}
);
}
});
}
}
});

app.factory('userFactory', function($q) {
return {
tryUsername: function(username) {

var deferred = $q.defer();
if (username == 'john')
deferred.reject('john already exists!');
else if (username == 'tim')
deferred.resolve('tim');
else if (username == 'hank')
deferred.reject('hank already exists!');
else
deferred.resolve(username);

return deferred.promise;
}
}

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app="app" ng-controller="ctrl">
<form name="form">
Enter a Username (john and hank are taken):
<br />
<input type="text" ng-model="username" name="username" unique /> <span ng-show="form.username.$error.unique">Username is taken!</span><br />
{{ form.username.$error }}
</form>
</body>

关于javascript - angularjs 1.2 中的异步验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27888176/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com