在Angular 1.2中,ngRoute是一个单独的模块,因此您可以使用其他社区路由器ui.router.
我正在编写一个开源模块,旨在为多种不同的路由器实现工作.那么如何检查哪个路由器已加载或存在?
我正在我的模块中的工厂内执行以下操作,但它不能按照我期望的方式工作:
if (angular.module("ngRoute"))
// Do ngRoute-specific stuff.
else if (angular.module("ui.router"))
// Do ui.router-specific stuff.
Run Code Online (Sandbox Code Playgroud)
对于未加载的模块,它会引发错误.例如,如果应用程序正在使用ui.router,则会为ngRoute检查引发以下错误:
未捕获错误:[$ injector:nomod]模块'ngRoute'不可用!您要么错误拼写了模块名称,要么忘记加载它.如果注册模块,请确保将依赖项指定为第二个参数.
假设我有一个 angular 应用程序,它有一些模块和一些指令。
我想获取此应用程序中所有已定义指令的列表。
例如:
var myModule = angular.module('myModule', [function(){...}]);
myModule.directive('myDirective', [function () {...}]);
myModule.directive('myOtherDirective', [function () {...}]);
var myOtherModule = angular.module('myOtherModule', [function(){...}]);
myOtherModule.directive('thirdDirective', [function () {...}]);
Run Code Online (Sandbox Code Playgroud)
我想编写一个getAllDirectives()将返回的函数['myDirective','myOtherDirective','thirdDirective']。
此外,如果可能的话,我还想知道每个指令属于哪个模块:
{
'myModule': ['myDirective','myOtherDirective'],
'myOtherModule':['thirdDirective']
}
Run Code Online (Sandbox Code Playgroud)
如何才能做到这一点?
请注意,我需要在应用程序本身之外并在它已经加载之后执行此操作。我确实可以访问页面上公开的任何内容,例如angular对象和DOM.