Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.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_Angular Ngmodel - Fatal编程技术网

Angularjs 获取异步验证程序返回的承诺的结果

Angularjs 获取异步验证程序返回的承诺的结果,angularjs,angular-ngmodel,Angularjs,Angular Ngmodel,angular文档中的asyncValidator示例如下所示: ngModel.$asyncValidators.uniqueUsername = function(modelValue, viewValue) { var value = modelValue || viewValue; // Lookup user by username return $http.get('/api/users/' + value). then(function resolved()

angular文档中的asyncValidator示例如下所示:

ngModel.$asyncValidators.uniqueUsername = function(modelValue, viewValue) {
  var value = modelValue || viewValue;

  // Lookup user by username
  return $http.get('/api/users/' + value).
     then(function resolved() {
       //username exists, this means validation fails
       return $q.reject('exists');
     }, function rejected() {
       //username does not exist, therefore this validation passes
       return true;
     });
};
正如您所看到的,当返回被拒绝的承诺时,它传递的值是“exists”。这似乎意味着我可以在使用模型时访问此值。这可能吗?

$http.get('/api/users/'+value)
看起来像一个常规的ajax请求。将第一个参数添加到success回调可能会使您能够访问返回的数据——如果您的api确实返回任何数据的话

then(function resolved(response) {
  console.log(response);
  return $q.reject('exists');
}

我也在想,因为我在这里没有找到答案,所以我看了一下源代码(对于AngularJS 1.7.5):

函数processAsyncValidators(){
var=[];
/* … */
forEach(即.$asyncValidators,函数(验证器,名称){
var promise=验证器(modelValue、viewValue);
/* … */
validatorPromises.push(promise.then(function()){
/* … */
},函数(){
/* … */
}));
});
/* … */
}
在这里,您可以看到asyncValidator返回的承诺与使用返回值的函数链接在一起


因此,不幸的是,无法访问返回值。

对,我不想知道如何获得任何旧承诺的结果,我正在尝试从验证管道之外获得承诺的结果,这意味着我无法访问原始承诺。