Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1c010b3

Browse files
docs($http): add examples when calling $http outside $apply
Closes angular#3996
1 parent 16c7ab1 commit 1c010b3

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

src/ng/http.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,32 @@ function $HttpProvider() {
186186
* XMLHttpRequest will transparently follow it, meaning that the error callback will not be
187187
* called for such responses.
188188
*
189-
* If your $http is scheduled from something that doesn't cause a $digest to fire then your
190-
* request won't be sent immediately. To make sure a $http request if fired immediately, wrap your
191-
* call around with an $scope.$apply(function(){ //make $http request here }
189+
* # Calling $http from outside AngularJS
190+
* The `$http` service will not actually send the request until the next `$digest()` is executed.
191+
* Normally this is not an issue, since almost all the time your call to `$http` will be from within
192+
* a `$apply()` block.
193+
* If you are calling `$http` from outside Angular, then you should wrap it in a call to `$apply`
194+
* to cause a $digest to occur and also to handle errors in the block correctly.
195+
*
196+
* ```
197+
* $scope.$apply(function() {
198+
* $http(...);
199+
* });
200+
* ```
201+
*
202+
* # Writing Unit Tests that use $http
203+
* When unit testing you are mostly responsible for scheduling the `$digest` cycle. If you do not
204+
* trigger a `$digest` before calling `$httpBackend.flush()` then the request will not have been
205+
* made and `$httpBackend.expect(...)` expectations will fail. The solution is to run the code
206+
* that calls the `$http()` method inside a $apply block as explained in the previous section.
207+
*
208+
* ```
209+
* $httpBackend.expectGET(...);
210+
* $scope.$apply(function() {
211+
* $http.get(...);
212+
* });
213+
* $httpBackend.flush();
214+
* ```
192215
*
193216
* # Shortcut methods
194217
*

0 commit comments

Comments
 (0)