0

I am trying to use custom services before the application bootstrap.

var initInjector = angular.injector(['ng', 'myModule']);

var myCustomService = initInjector.get('myCustomService');

var $http = initInjector.get('$http');

var $q = initInjector.get('$q');

And then in the myCustomService I got:

angular.module('myModule').service('myCustomService',myCustomService);

 function myCustomService($location, $q){
   // some logic
 }

The error I got is :

Uncaught Error: [$injector:unpr] Unknown provider: $rootElementProvider <- $rootElement <- $location <- myCustomService <- $location

It seems that the dependencies used in myCustomService cannot be loaded..

There is a way to organise better the pre bootstrap code and logic?

soumya
  • 3,801
  • 9
  • 35
  • 69

1 Answers1

0

I know this is a late answer, but from the error message posted in the question, it looks like $location should be available before the application bootstrap.

To get the $location before bootstrapping, you would need to provide the $rootElement. One way is to mock it: inject ngMock module from angular-mocks to get the injector.

var initInjector = angular.injector(['ng', 'myModule', 'ngMock']);
var $location = initInjector.get('$location');

Also check out this StackOverflow post and this GitHub thread.

Dhruv Saraswat
  • 858
  • 9
  • 13