Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using angular services before manual bootstrap

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');
like image 430
Michael Allan Jackson Avatar asked Dec 04 '13 01:12

Michael Allan Jackson


1 Answers

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');
});
like image 180
Brian Genisio Avatar answered Oct 22 '22 09:10

Brian Genisio