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

Why does $http outside of $apply actually work? #5206

Closed
cburgdorf opened this issue Nov 29, 2013 · 11 comments
Closed

Why does $http outside of $apply actually work? #5206

cburgdorf opened this issue Nov 29, 2013 · 11 comments
Assignees

Comments

@cburgdorf
Copy link
Contributor

The documentation says that it should not work outside of $apply phase but yet it seems to work fine from outside of an $apply phase as proven by this plunkr:

http://plnkr.co/edit/2EXpWkYQuxCgPZ5x9JOu?p=preview

@0x-r4bbit
Copy link
Contributor

@cburgdorf because it runs an $apply for you internally. Can you point to the place in the docs?

@cburgdorf
Copy link
Contributor Author

It's not supposed to work like this. Can you point me to the code where it internally calls $apply? Here is how the docs explain that it should not work outside from a $digest.

https://github.com/angular/angular.js/blob/master/src/ng/http.js#L226

And I can confirm that it actually needed to be called from within $apply somewhere before 1.2. So I actually think it has something to do with the new $postDigestQueue (not sure about the correct name).

@caitp
Copy link
Contributor

caitp commented Dec 1, 2013

I believe they're referring to

The $http service will not actually send the request until the next $digest() is executed. Normally this is not an issue, since almost all the time your call to $http will be from within a $apply() block. If you are calling $http from outside Angular, then you should wrap it in a call to $apply to cause a $digest to occur and also to handle errors in the block correctly.

https://github.com/angular/angular.js/blob/master/src/ng/http.js#L227

@revolunet
Copy link
Contributor

The $apply call takes place here, just after the request is finished : https://github.com/angular/angular.js/blob/master/src/ng/http.js#L991

But this is triggered after the $http call. the HTTP call itself wasnt triggered if called from outside Angular before 1.2.0.

When using 1.1.5 in your plunk, the HTTP call is not even triggered so yes, something changed lately and i didnt find the revelant commit or explanation yet. good question :)

@cburgdorf
Copy link
Contributor Author

@revolunet exactly. I also couldn't find a commit which should have changed it. Also the documentation says it shouldn't be that way. So I assume it has something to do with the $postDigestQueue which was introduced somewhere in between.

@revolunet
Copy link
Contributor

my friend git bisect just told me that it comes from this commit : 6b91aa0

@cburgdorf
Copy link
Contributor Author

This makes it sound as if it's intended behavior in which we should remove the critical part from the documentation :)

@0x-r4bbit
Copy link
Contributor

Well let's give @IgorMinar the chance to explain what's going on here :)

@ghost ghost assigned btford Dec 19, 2013
@btford
Copy link
Contributor

btford commented Dec 19, 2013

This is intended behavior. 6b91aa0 was introduced to fix an issue with $http response interceptors.

+1 for updating the docs (any volunteers?).

@btford btford closed this as completed Dec 19, 2013
@0x-r4bbit
Copy link
Contributor

Thanks for clarifying @btford

cburgdorf added a commit to cburgdorf/angular.js that referenced this issue Dec 19, 2013
This removes some outdated advice which no longer is true against the latest angular version.

Relates to angular#5206
@cburgdorf
Copy link
Contributor Author

Done #5485

caitp pushed a commit that referenced this issue Jan 19, 2014
This removes some outdated advice which no longer is true against the latest angular version.

The information about unit testing with ngMocks remains, because it's always good to have
information like that easily found. This little snippet is not worded perfectly, and is not
a very good example unit test, so additional work is needed here.

Relates to #5206
Closes #5485
jamesdaily pushed a commit to jamesdaily/angular.js that referenced this issue Jan 27, 2014
This removes some outdated advice which no longer is true against the latest angular version.

The information about unit testing with ngMocks remains, because it's always good to have
information like that easily found. This little snippet is not worded perfectly, and is not
a very good example unit test, so additional work is needed here.

Relates to angular#5206
Closes angular#5485
jamesdaily pushed a commit to jamesdaily/angular.js that referenced this issue Jan 27, 2014
This removes some outdated advice which no longer is true against the latest angular version.

The information about unit testing with ngMocks remains, because it's always good to have
information like that easily found. This little snippet is not worded perfectly, and is not
a very good example unit test, so additional work is needed here.

Relates to angular#5206
Closes angular#5485
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants