未知提供者:在 angular.bootstrap 之前使用 $injector 获取 $location 服务时的 $rootElementProvider [英] Unknown provider: $rootElementProvider when using $injector to get $location service before angular.bootstrap
问题描述
您好,我正在尝试手动引导一个 Angular 应用程序,但有一些业务需要先处理.这篇 文章提到了我感兴趣的技术.
Hello I am trying to manually bootstrap an angular app, but there is some business that needs to be taken care of first.This article mentions the technique I am interested in.
当我注入这个时:
var $injector = angular.injector(["ng"]);
var $http = $injector.get("$http");
它工作正常,但这个:
var $injector= angular.injector(["ng", "myApp"]);
var $location = $injector.get("$location");
抛出以下错误.
Uncaught Error: [$injector:unpr] Unknown provider: $rootElementProvider <- $rootElement <- $location <- $location
是否可以在 angular.bootstrap 之前获得 $location?
Is it possible to get $location prior to angular.bootstrap?
非常感谢!
推荐答案
为了在引导前获得 $location
,您基本上需要提供 $rootElement
.一种方法是模拟它:从 angular-mocks
注入 ngMock
模块以获取注入器.
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");
或者通过创建一个模拟应用程序并在获取注入器时包含它来自己提供 rootElement.
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");
或者其他方式(根据您的可行性)是使用 window.location.
Or other way (based on your feasibility) is to obtain the location from the window using window.location.
另外值得注意的是 github
这篇关于未知提供者:在 angular.bootstrap 之前使用 $injector 获取 $location 服务时的 $rootElementProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!