5

I would like to use angular services before doing a manual bootstrap(the ng-app directive is not being used). Is there a way to access angular services without calling angular.bootstrap on an empty div?

The following approach works, but I'm wondering if there is a more direct solution:

  var element = angular.element('<div></div>');
  angular.bootstrap(element);
  var $injector = element.injector();
  var $http = $injector.get('$http');
  var $location = $injector.get('$location');
Michael Allan Jackson
  • 4,217
  • 3
  • 35
  • 45

1 Answers1

5

There isn't a way to do it without calling bootstrap (or getting the injector from an already bootstrapped element), but there is a slightly more direct way of doing it:

var $http = angular.bootstrap().get('$http');

Or, if you need multiple services, you can do this:

angular.bootstrap().invoke(function($http, $location){ 
    $http.get('/foo');
    $location.path('/foo');
});
Brian Genisio
  • 47,787
  • 16
  • 124
  • 167
  • Thanks! I hadn't looked at invoke or the fact that bootstrap returns the injector, very helpful. Playing around some more, it looks like angular.bootstrap() works fine with no arguments. – Michael Allan Jackson Dec 04 '13 at 04:51
  • Cool. I've updated my answer. I thought I had tried that and got an error, but I tried again and it worked, so I was mistaken. – Brian Genisio Dec 04 '13 at 10:35