无法检索从角注射器 [英] Can't retrieve the injector from angular

查看:126
本文介绍了无法检索从角注射器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个程序有两个模块:

I've got this app with two modules:

angular.module('components', []).directive('foo', function () { return {};});
angular.module('gaad', ['components']);

有一帮这个模块,我不是在这里包括相关的指令。
该应用程序工作正常。然而,当我试图检索模块 gaad 喷油器:

There is a bunch of directives associated with this modules which I'm not including here. The application works fine. However when I'm trying to retrieve injector for module gaad:

var injector = angular.injector(['gaad', 'components']); //called after 'gaad' module initialization

错误被抛出:

Uncaught Error: Unknown provider: $compileProvider from components 

应用程序现在是相当大的,我不知道我应该在哪里寻找错误。
所以我的问题是:有什么能为我的问题的原因

The application is quite big right now and I have no idea where should I look for bugs. So my question is: What could be the reason for my problems?

编辑:
我能复制我的问题: http://jsfiddle.net/selbh/ehmnt/11/

推荐答案

回答,我们需要注意,只有一个注射器每个应用程序实例的问题之前,而不是每个模块。在这个意义上说,不可能检索每个模块的喷射器。当然,如果我们采取顶层模块,它重新presents整个应用程序。在这个意义上说,应用程序和顶层模块似乎等效。这似乎是一个微妙的差异,但为了全面正确回答这个问题,了解这一点很重要。

Before answering the question we need to note that there is only one injector instance per application and not per module. In this sense it is not possible to retrieve an injector per module. Of course if we take the top-level module, it represents the whole application. In this sense, an application and top-level module seem equivalent. It might seem like a subtle difference but it is important to understand in order to fully and properly answer this question.

接下来,从我能理解你想的检索的的 $注射器,而不是创建一个新的实例。问题是,在 angular.injector 将创建一个新的 $喷射器模块指定为参数实例(应用程序) 。这里的主要AngularJS模块(纳克)必须明确指定。所以,在这个code例如:

Next, from what I can understand you would like to retrieve the $injector and not create a new instance of it. The thing is that the angular.injector will create a new $injector instance for modules (an app) specified as arguments. Here the main AngularJS module (ng) must be specified explicitly. So, in this code example:

var injector = angular.injector(['gaad', 'components']);

您正试图创建从gaad'和'组件'模块,显然定义组件的新注射器 $ compileProvider 是不是在你的自定义模块定义。 添加纳克模块的名单将通过创建一个新的注射器解决的问题 - 这你可能不希望发生的。

you were trying to create a new injector from components defined in 'gaad' and 'components' modules and obviously $compileProvider is not defined in your custom modules. Adding the ng module to the list would "solve" the problem by creating a new injector - something that you probably don't want to happen.

要实际获取与正在运行的应用注射器实例我们可以使用两种方法:

To actually retrieve an injector instance associated to a running application we can use 2 methods:


    从AngularJS在
  • JavaScript的最简单的办法就是注入 $注射器实例:<一href=\"http://docs.angularjs.org/api/angular.injector\">http://docs.angularjs.org/api/angular.injector

  • 从AngularJS世界之外的
  • - 电话 angular.element([DOM元素])喷油器()其中[DOM元素]是一个圆顶的元素,其中 NG-应用定义(或者该元素的任何子元素)。更多资讯:<一href=\"http://docs.angularjs.org/api/angular.element\">http://docs.angularjs.org/api/angular.element

  • from within AngularJS JavaScript the simplest solution is just to inject $injector instance: http://docs.angularjs.org/api/angular.injector
  • from outside of AngularJS world - call angular.element([DOM element]).injector() where [DOM element] is a dome element where the ng-app was defined (or any child element of this element). More info here: http://docs.angularjs.org/api/angular.element

下面是该注射器检索2方法的jsfiddle: http://jsfiddle.net/xaQzb/

Here is the jsFiddle showing 2 methods of the injector retrieval: http://jsfiddle.net/xaQzb/

另外请注意,使用 $注射器并不能直接使用单元测试之外很常见的场景。这可能是有用的思想从AngularJS世界之外的检索AngularJS服务。更多信息在这里:从传统code 调用JS角

Please also note that using $injector directly is not very common scenario outside of unit testing. It might be useful thought for retrieving AngularJS services from outside of AngularJS world. More info here: Call Angular JS from legacy code.

这篇关于无法检索从角注射器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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