如何在某些父模块中获取依赖模块列表?
How get a list of dependent modules in within some parent module?
设置:
几个 angularjs 模块声明为:
angular.module('a', ['a.b']);
angular.module('a.b', ['a.b.c1', 'a.b.c2']);
angular.module('a.b.c1', []);
angular.module('a.b.c2', []);
问题:
有没有办法从特定父模块获取所有子模块?
我的意思是:
angular.module.getChildrenFromParent('a.b');
那应该 return 一个字符串数组或对模块 a.b.c1
和 a.b.c2
.
的引用数组
谢谢
更新
我知道有 How can I get a list of available modules in AngularJS? 但它不是一个干净的解决方案,它也是一个有点老的答案。
您可以使用返回的模块对象的requires
属性(通过angular.module
getter 调用)来获取依赖列表。
即
var dependentModules = angular.module('a.b').requires;
这将输出表示依赖项的字符串数组 ["a.b.c1", "a.b.c2"]
。但请注意,您只能在创建模块后使用它,否则为尚未创建的模块调用它会导致错误。如果您想深入了解,只需为每个依赖项实现递归查找。
演示
angular.module('a.b.c1', []);
angular.module('a.b.c2', []);
angular.module('a.b', ['a.b.c1', 'a.b.c2']);
angular.module('a', ['a.b']);
console.log(angular.module('a.b').requires);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
设置:
几个 angularjs 模块声明为:
angular.module('a', ['a.b']);
angular.module('a.b', ['a.b.c1', 'a.b.c2']);
angular.module('a.b.c1', []);
angular.module('a.b.c2', []);
问题:
有没有办法从特定父模块获取所有子模块? 我的意思是:
angular.module.getChildrenFromParent('a.b');
那应该 return 一个字符串数组或对模块 a.b.c1
和 a.b.c2
.
谢谢
更新
我知道有 How can I get a list of available modules in AngularJS? 但它不是一个干净的解决方案,它也是一个有点老的答案。
您可以使用返回的模块对象的requires
属性(通过angular.module
getter 调用)来获取依赖列表。
即
var dependentModules = angular.module('a.b').requires;
这将输出表示依赖项的字符串数组 ["a.b.c1", "a.b.c2"]
。但请注意,您只能在创建模块后使用它,否则为尚未创建的模块调用它会导致错误。如果您想深入了解,只需为每个依赖项实现递归查找。
演示
angular.module('a.b.c1', []);
angular.module('a.b.c2', []);
angular.module('a.b', ['a.b.c1', 'a.b.c2']);
angular.module('a', ['a.b']);
console.log(angular.module('a.b').requires);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>