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

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

问题描述

这个应用有两个模块:

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/

推荐答案

在回答问题之前,我们需要注意每个应用程序只有一个注入器实例,而不是每个模块.从这个意义上说,不可能检索每个模块的注入器.当然如果我们拿顶层模块来说,它代表了整个应用程序.从这个意义上说,应用程序和顶级模块似乎是等效的.这似乎是一个微妙的区别,但为了充分、正确地回答这个问题,理解这一点很重要.

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.

接下来,据我所知,您希望检索 $injector 而不是创建它的新实例.问题是 angular.injector 将为指定为参数的模块(应用程序)创建一个新的 $injector 实例.这里必须明确指定主要的 AngularJS 模块 (ng).因此,在此代码示例中:

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' 和 'components' 模块中定义的组件创建一个新的注入器,显然 $compileProvider 未在您的自定义模块中定义.ng 模块添加到列表中将通过创建新的注入器来解决"问题 - 这可能是您不希望发生的事情.

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:

  • 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

这是 jsFiddle,显示了注入器检索的 2 种方法:http://jsfiddle.net/xaQzb/

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

另请注意,在单元测试之外,直接使用 $injector 并不是很常见的场景.从 AngularJS 世界之外检索 AngularJS 服务可能是有用的想法.更多信息:从旧代码调用 Angular 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天全站免登陆