角 - 获取所有注册的服务列表? [英] Angular - Get List of All Registered Services?
问题描述
我可以得到所有已注册的指令,服务,控制器等的列表的在运行时的。 。 。 ?
Can I get a list of all registered directives, services, controllers, etc. at runtime . . . ?
推荐答案
您可以得到供应商的名单(即服务/指令/控制器/工厂/等),每个模块,虽然名单是种神秘的。
You can get a list of the providers (ie services/directives/controllers/factories/etc) for each module, although the list is kind of cryptic.
假设你有以下内容:
var mod = angular.module('myModule', []);
mod.factory('fact1', function($dependency1, $dependency2){ ... });
mod.service('serv1', function($dependency3, $dependency4){ ... });
mod.controller('ctrl1', function($dependency2, $dependency3){ ... });
mod.factory('fact2', function($dependency1, $dependency4){ ... });
...
然后 MOD
变量将包含一个名为 mod._invokeQueue
属性将包含所有提供的数组属于该模块的一部分。在 _invokeQueue
会是这个样子:
Then the mod
variable will contain an attribute called mod._invokeQueue
that will contain an array of all the providers that are part of that module. The _invokeQueue
will look something like this:
[
['$provide', 'factory', Arguments['fact1', ['$dependency1', '$dependency2', function(){}],
['$provide', 'service', Arguments['serv1', ['$dependency3', '$dependency4', function(){}],
['$provide', 'controller', Arguments['ctrl1', ['$dependency2', '$dependency3', function(){}],
['$provide', 'factory', Arguments['fact2', ['$dependency1', '$dependency4', function(){}]
...
]
所以,你可以通过 mod._invokeQueue
为它所包含的每个供应商进行搜索。
So you can search through that mod._invokeQueue
for each provider that it contains.
但是,这将只包含提供该特定模块的列表。如果你想获得所有相关的模块列表,你将需要通过 mod.requires
数组循环。
But that will only contain the list of providers for that specific module. If you want to get a list of all of the dependent modules, you will need to loop through the mod.requires
array.
如果该模块具有模块级的依赖关系,像这样:
If the module has module-level dependencies, like so:
var mod = angular.module('myModule', ['otherModule1','otherModule2']);
然后 MOD
对象也将包含这些模块依赖的名称的 mod.requires
阵列,像这样:
Then the mod
object will also have a mod.requires
array that contains the names of those module dependencies, like so:
angular.forEach(mod.requires, function(requiredModuleName){
// first get a reference to the required module by calling angular.module()
var requiredMod = angular.module(requiredModuleName);
// requiredMod will have its own ._invokeQueue
// requiredMod._invokeQueue will look like the _invokeQueue from above
...
// do something with the additional providers in _invokeQueue
});
希望有所帮助。
这篇关于角 - 获取所有注册的服务列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!