Unknown provider: $ rootElementProvider <- $ rootElement <- $ location in angulartics.js

I am using angulartics in my project, but when I add a dependency on my module, I get the following error:

Unknown provider: $ rootElementProvider <- $ rootElement <- $ location.

I have connected angulartics.js

in html afterangular.js

It is generated in .run:

code of lib here: https://github.com/luisfarzati/angulartics/issues/203

$location

is a good object, but $rootElementProvider

also $rootElement

undefined.

How to solve this problem?

+3


source share


1 answer


I recently got this same issue, the only reason I know this can happen is when you create the angular js injector manually, which depends on the module that injects $ location during bootstrap or when you try to get $ location through this injector.

This issue is not actually related to the angulartics library itself, but rather to angular's own $ location service, which has a direct dependency on $ rootElement, which is defined at boot time of the application, so does not exist before the application starts.

There is one easy way to fix this if you have this angulartics problem and that is to remove angulartics as a dependency on your application and add it as a dependency of the resumeBootstrap method, which allows us to add more runtime dependencies when resuming the angular bootstrap process.

For example:



angular.module('myApp', [
  // array of dependencies without angulartics
]);
var preBootstrapInjector = angular.injector(['ng', 'myApp']);
var $rootScope = preBootstrapInjector.$get('$rootScope');
var myService = preBootstrapInjector.$get('myService');
myService.getDataFromServer()
  .then(doSomethingWithThatData)
  .then(resumeBootstrap);
    
function resumeBootstrap(){
  // Clean up the custom injector for garbage collection
  $rootScope.$destroy();
  
  // Resume Angular bootstrap process
  $(document).ready(function() {
    angular.resumeBootstrap([
      // dependencies from modules that need $location
      'angulartics'
    ]);
  });
}
      

Hooray!

0


source







All Articles