Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 异步消息验证程序_Angularjs_Ng Messages - Fatal编程技术网

Angularjs 异步消息验证程序

Angularjs 异步消息验证程序,angularjs,ng-messages,Angularjs,Ng Messages,我正在尝试编写指令来验证服务器上是否存在用户名。 如果存在,我希望通过ng消息显示错误。 但由于某些原因,错误并没有显示。我检查了服务器响应,它返回了正确的值,但验证程序似乎无法解析promise等等 //指令 角度.module('main').directive('checkIfUserExists',checkIfUserExists); checkIfUserExists.$inject=['authService']; 函数checkIfUserExists(authService)

我正在尝试编写指令来验证服务器上是否存在用户名。 如果存在,我希望通过ng消息显示错误。 但由于某些原因,错误并没有显示。我检查了服务器响应,它返回了正确的值,但验证程序似乎无法解析promise等等

//指令
角度.module('main').directive('checkIfUserExists',checkIfUserExists);
checkIfUserExists.$inject=['authService'];
函数checkIfUserExists(authService){
返回{
要求:“ngModel”,
链接:功能(范围、元素、属性、模型){
ngModel.$asyncValidators.checkIfUserExists=函数(modelValue){
返回authService.suchUserReadyExists(modelValue);
};
}
};
};
//服务方式
suchUserReadyExists:函数(用户){
返回$http.get(baseUrl+“/api/account/someone/”+user){
console.log(response.data);
return!response.data;
},函数(数据、状态、标题、配置){
返回$q.reject('发生错误');
});
}
//我要验证的输入
此字段必填
只允许使用字母数字字符
用户名不能超过10个字符
用户名必须超过6个字符
这样的用户已经存在

从$asyncValidators函数返回需要是一个承诺。拒绝承诺“未通过”验证,承诺的解决“通过”验证。 工作小提琴

var myApp=angular.module('myApp',['ngMessages']);
myApp.controller('MyCtrl',['$scope',',
函数($scope){}
]);
myApp.directive('checkIfUserExists'[
“$q”,
“$http”,
函数($q,$http){
返回{
限制:“A”,
要求:'ngModel',
链接:功能(范围、元素、属性、模型){
ngModel.$asyncValidators.checkIfUserExists=函数(modelValue){
var defer=$q.defer();
$http.get(baseUrl+“/api/account/someone/“+user)。然后(函数(isUnique){
if(response.data){
defer.resolve();
}否则{
defer.reject();
}
}).catch(函数(err){
//拒绝并显示错误消息
延迟、拒绝(错误);
});
//还债
回报、承诺;
};
}
};
}
]);

这样的用户已经存在

检查一下,看看是否有帮助。工作小提琴你的小提琴工作完美!问题是我认为我可以为resolve方法设置true/false值。看起来resolve-为true,reject-为false。您能否将您的评论作为答案发布,以便我将其标记为正确答案?多谢各位!