AngularJS-获取所有注册服务的列表? [英] AngularJS - Get List of All Registered Services?

查看:96
本文介绍了AngularJS-获取所有注册服务的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在运行时获取所有已注册指令,服务,控制器等的列表吗? . . . ?

解决方案

您可以获取每个模块的提供程序列表(例如,服务/指令/控制器/工厂/等),尽管该列表有点含糊./p>

假设您具有以下条件:

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看起来像这样:

[
    ['$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中搜索其中包含的每个提供程序.

但是,它将仅包含该特定模块的提供程序列表.如果要获取所有相关模块的列表,则需要遍历mod.requires数组.

如果模块具有模块级别的依存关系,例如:

var mod = angular.module('myModule', ['otherModule1','otherModule2']);

然后,mod对象还将具有一个mod.requires数组,其中包含那些模块依赖项的名称,如下所示:

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

希望有帮助.

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.

Say you have the following:

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){ ... });
...

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(){}]
    ...
]

So you can search through that mod._invokeQueue for each provider that it contains.

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']);

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

Hope that helps.

这篇关于AngularJS-获取所有注册服务的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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