The asyncValidator example in the angular docs looks like this:
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;
});
};
As you can see, when returning a rejected promise it's passing the value "exists". This seems to imply that I can get access to this value when working with the model. Is this possible?
I was wondering as well, and since I did not find the answer here, I had a look at the source code (for AngularJS 1.7.5):
function processAsyncValidators() {
var validatorPromises = [];
/* … */
forEach(that.$asyncValidators, function(validator, name) {
var promise = validator(modelValue, viewValue);
/* … */
validatorPromises.push(promise.then(function() {
/* … */
}, function() {
/* … */
}));
});
/* … */
}
Here, you can see that the promise that is returned by the asyncValidator is chained with functions that do not use the returned value.
Hence, unfortunately, there is no way to access the returned values.