19

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.

Community
  • 1
  • 1
PSWai
  • 1,198
  • 13
  • 32
  • 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. – Michelle Tilley Sep 04 '13 at 03: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! – PSWai Sep 05 '13 at 03: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. :) – Michelle Tilley Sep 05 '13 at 03:45
  • @BrandonTilley Me too thought it will work without a digest cycle, at least not for sending xhr. :) – PSWai Sep 05 '13 at 04:00

1 Answers1

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

then

$http.get(...) // or post or whatever
dustin.schultz
  • 13,076
  • 7
  • 54
  • 63
  • 2
    Loved that! I even don't know why the person who asked doesn't assign that as a correct answer! – Islam Attrash May 16 '16 at 14:20
  • 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!! – George Feb 07 '18 at 12:08