Using Angularjs $http in browser console

All we need is an easy explanation of the problem, so here it is.

I’ve testing AngularJS services in browser console during development for quick verification. The way I inject a service into console is as describe in this question or

var $inj = angular.injector(['myApp']);
var serv = $inj.get('myService');
serv.doSomething();

That was working perfectly with AngularJS 1.0.7. However, after upgrading to 1.1.5, it doesn’t work anymore for services that uses $http service, that no xhr will be sent.

I’ve tested injecting $http directly, it also doesn’t work. AngularJS changelog seems to have no record on this issue. May I know what’s the problem here?

Update:

It seems like AngularJS 1.0.7 Uncompressed version doesn’t work as well. Currently the tested working version is only AngularJS 1.0.7 Minified.

It works for Uncompressed also.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

$http = angular.element(document.body).injector().get('$http');

then

$http.get(...) // or post or whatever

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply