我怎样才能在AngularJS可用模块的列表? [英] How can I get a list of available modules in AngularJS?

查看:95
本文介绍了我怎样才能在AngularJS可用模块的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在限定角模块,我定义什么是我的依赖关系是这样的:
VAR MyModule的= angular.module(MyModuleName,[DEP1,Dep2,Dep3]);

When defining an Angular module I define what are my dependencies like this: var myModule = angular.module("MyModuleName", ["Dep1", "Dep2", "Dep3"]);

每个依赖都有其自己的依赖,指令控制器等。

Each dependency has its own dependencies, directives, controllers etc.

有没有办法问AngularJS哪些是可用注射?如果没有他们的实例,刚开列表。

Is there a way to ask AngularJS what are the available injectables? Without instantiating them, just getting a list.

该角。$喷油器只有一个get方法,但是这意味着我将它实例化。

The Angular.$injector only has a "get" method, but this means that I'll instantiate it.

谢谢
吉尔阿姆兰

Thanks Gil Amran

推荐答案

其实这是一种黑客攻击的,所以用它进行测试和学习的目的!

Actually that's kind of a hack , so only use it for testing and learning purposes!!

所有$注射器DI魔术保持这个文件中:

All the $injector DI magic is kept inside this file:

https://github.com/角/ angular.js / BLOB / v1.2.7 / src目录/自动/ injector.js

看到createInjector功能(订单598:angularjs 1.2.7

see the createInjector function (line 598 : angularjs 1.2.7)


  • providerCache 变量是容器关什么配置块可用至$注射器。

  • InstanceCache控制变量是容器关什么其他所有模块可用至$注射器。

  • The providerCache variable is the container off what's available to $injector in config blocks.
  • The instanceCache variable is the container off what's available to $injector in all other blocks.
function createInjector(modulesToLoad) {
  var INSTANTIATING = {},
      providerSuffix = 'Provider',
      path = [],
      loadedModules = new HashMap(),
      providerCache = {
        $provide: {
            provider: supportObject(provider),
            factory: supportObject(factory),
            service: supportObject(service),
            value: supportObject(value),
            constant: supportObject(constant),
            decorator: decorator
          }
      },
      providerInjector = (providerCache.$injector =
          createInternalInjector(providerCache, function() {
            throw $injectorMinErr('unpr', "Unknown provider: {0}", path.join(' <- '));
          })),
      instanceCache = {},
      instanceInjector = (instanceCache.$injector =
          createInternalInjector(instanceCache, function(servicename) {
            var provider = providerInjector.get(servicename + providerSuffix);
            return instanceInjector.invoke(provider.$get, provider);
          }));

这些变量以及封装的闭包内,你不能从外部得到它们。

Those variables are well encapsulated inside that closure and you cannot get them from outside.

除非你这两行添加到createInjector功能:

unless you add these two lines to the createInjector function:

window.providerCache = providerCache;
window.instanceCache = instanceCache;

如何使用它:


  • 从这里下载源$ C ​​$ C:的http:// code.angularjs.org / 1.2.7 / angular.js

  • 在行添加这两条线的 3549

  • How to use it:

    • Download the source code from here: http://code.angularjs.org/1.2.7/angular.js
    • Add these 2 lines at line 3549
    • 这篇关于我怎样才能在AngularJS可用模块的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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