Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit bba6004

Browse files
committed
docs(ngModel): rename $asyncValidators error to nopromise and add missing error page
Closes #13795
1 parent d386b7a commit bba6004

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
@ngdoc error
2+
@name ngModel:nopromise
3+
@fullName No promise
4+
@description
5+
6+
The return value of an async validator, must always be a promise. If you want to return a
7+
non-promise value, you can convert it to a promise using {@link ng.$q#resolve `$q.resolve()`} or
8+
{@link ng.$q#reject `$q.reject()`}.
9+
10+
Example:
11+
12+
```
13+
.directive('asyncValidator', function($q) {
14+
return {
15+
require: 'ngModel',
16+
link: function(scope, elem, attrs, ngModel) {
17+
ngModel.$asyncValidators.myAsyncValidation = function(modelValue, viewValue) {
18+
if (/* I don't need to hit the backend API */) {
19+
return $q.resolve(); // to mark as valid or
20+
// return $q.reject(); // to mark as invalid
21+
} else {
22+
// ...send a request to the backend and return a promise
23+
}
24+
};
25+
}
26+
};
27+
})
28+
```

src/ng/directive/ngModel.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
596596
forEach(ctrl.$asyncValidators, function(validator, name) {
597597
var promise = validator(modelValue, viewValue);
598598
if (!isPromiseLike(promise)) {
599-
throw ngModelMinErr("$asyncValidators",
599+
throw ngModelMinErr('nopromise',
600600
"Expected asynchronous validator to return a promise but got '{0}' instead.", promise);
601601
}
602602
setValidity(name, undefined);

test/ng/directive/ngModelSpec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,7 @@ describe('ngModel', function() {
904904

905905
expect(function() {
906906
scope.$apply('value = "123"');
907-
}).toThrowMinErr("ngModel", "$asyncValidators",
907+
}).toThrowMinErr("ngModel", "nopromise",
908908
"Expected asynchronous validator to return a promise but got 'true' instead.");
909909
}));
910910

0 commit comments

Comments
 (0)