18

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.

4
  • 1
    Does it work if you get access to the $rootScope service and call $rootScope.$apply() ? I ran into a sort-of-similar issue the other day, and kicking off a digest cycle was the fix. Commented Sep 4, 2013 at 3:51
  • @BrandonTilley That works fantastically! So is$http changed to be like $q that will only be resolved in a digest cycle? I think I should read the changelog more closely. Thanks! Commented Sep 5, 2013 at 3:37
  • Yes, I think so. I thought maybe I had imagined it working without a digest cycle, but you've helped me realize I'm not out of my mind (at least not because of this. :) Commented Sep 5, 2013 at 3:45
  • @BrandonTilley Me too thought it will work without a digest cycle, at least not for sending xhr. :) Commented Sep 5, 2013 at 4:00

1 Answer 1

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

then

$http.get(...) // or post or whatever
Sign up to request clarification or add additional context in comments.

2 Comments

Loved that! I even don't know why the person who asked doesn't assign that as a correct answer!
ah damn that's awesome! We've got Marketeers using Optimizely to run AB tests and now there's no need to include heavy jQuery too!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.