未知提供程序:使用$ injector在angular.bootstrap之前获取$ location服务时的$ rootElementProvider [英] Unknown provider: $rootElementProvider when using $injector to get $location service before angular.bootstrap

查看:92
本文介绍了未知提供程序:使用$ injector在angular.bootstrap之前获取$ location服务时的$ rootElementProvider的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试手动引导角度应用程序,但是首先需要处理一些事务.

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");

柱塞

Plunker

或者通过创建一个模拟应用程序并在获取注射器时将其包括在内,自行提供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");

柱塞

Plunker

或者其他方法(根据您的可行性)是使用 window.location .

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

也值得在 github

这篇关于未知提供程序:使用$ injector在angular.bootstrap之前获取$ location服务时的$ rootElementProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆