Unknown provider: $rootElementProvider when using $injector to get $location service before angular.bootstrap

故事扮演 提交于 2019-12-03 11:53:16
PSL

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

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

Plunker

Or supply rootElement on your own by creating a mock app and including that while getting the injector.

var mockApp = angular.module('mockApp', []).provider({
  $rootElement:function() {
     this.$get = function() {
       return angular.element('<div ng-app></div>');
    };
  }
});

var $injector= angular.injector(['ng','mockApp',"plunker"]);

var $location = $injector.get("$location");

Plunker

Or other way (based on your feasibility) is to obtain the location from the window using window.location.

Also worthwhile noting this thread on github

root element must be in inserted in document. See http://plnkr.co/edit/OrgStgw4NpjU2LcIFXsB

var rootElement = angular.element(document);

var mockApp = angular.module('mockApp', []).provider({
  $rootElement:function() {
     this.$get = function() {
       return rootElement;
    };
  }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!