In angularjs, given a module, how do you check if a directive/controller exists given a module.
在angularjs中,给定一个模块,如何检查给定模块的指令/控制器是否存在。
I have a module and I want to know if some particular directives have been loaded. Below is some sample code:
我有一个模块,我想知道是否加载了一些特定的指令。下面是一些示例代码:
var module = angular.module('myModule');
//check if controller exists
if (module.hasController('my.first.controller')){
//do something
}
if (module.hasDirective('my.first.directive')){
//do something
}
I have implemented this in a way. Looking for a better way of doing it if it is available by default.
我以某种方式实现了这一点。寻找一种更好的方法,如果它是默认可用的。
Is this possible? If so, how do you do this?
这是可能的吗?如果是的话,你怎么做呢?
4 个解决方案
#1
13
Use this code to check if a service exists.
使用此代码检查服务是否存在。
$injector.has('myServiceName')
injector.has美元(“myServiceName”)
To check if a directive exists, you must add a Directive
suffix after the directive name:
要检查指令是否存在,您必须在指令名之后添加一个指令后缀:
$injector.has('myDirectiveNameDirective')
injector.has美元(“myDirectiveNameDirective”)
#2
3
I found some working code here
我在这里找到了一些工作代码
angular.service('ControllerChecker', ['$controller', function($controller) {
return {
exists: function(controllerName) {
if(typeof window[controllerName] == 'function') {
return true;
}
try {
$controller(controllerName);
return true;
} catch (error) {
return !(error instanceof TypeError);
}
}
};
}]);
JSFiddle: http://jsfiddle.net/fracz/HB7LU/6780/
JSFiddle:http://jsfiddle.net/fracz/HB7LU/6780/
#3
2
var controllers = [];
_.each(app._invokeQueue, function(value, index) {
if (value[0] !== '$controllerProvider') {
return;
}
controllers.push(value[2][0]);
});
if (controllers.indexOf('controller-i-want') === - 1) {
// controller is undefined
}
#4
-3
Solved the problem by writing a wrapper function that is called to load the controllers and stuff and at such I'm able to tell when each directive is loaded.
通过编写一个包装函数来加载控制器之类的东西来解决这个问题,这样我就可以知道何时加载每个指令。
#1
13
Use this code to check if a service exists.
使用此代码检查服务是否存在。
$injector.has('myServiceName')
injector.has美元(“myServiceName”)
To check if a directive exists, you must add a Directive
suffix after the directive name:
要检查指令是否存在,您必须在指令名之后添加一个指令后缀:
$injector.has('myDirectiveNameDirective')
injector.has美元(“myDirectiveNameDirective”)
#2
3
I found some working code here
我在这里找到了一些工作代码
angular.service('ControllerChecker', ['$controller', function($controller) {
return {
exists: function(controllerName) {
if(typeof window[controllerName] == 'function') {
return true;
}
try {
$controller(controllerName);
return true;
} catch (error) {
return !(error instanceof TypeError);
}
}
};
}]);
JSFiddle: http://jsfiddle.net/fracz/HB7LU/6780/
JSFiddle:http://jsfiddle.net/fracz/HB7LU/6780/
#3
2
var controllers = [];
_.each(app._invokeQueue, function(value, index) {
if (value[0] !== '$controllerProvider') {
return;
}
controllers.push(value[2][0]);
});
if (controllers.indexOf('controller-i-want') === - 1) {
// controller is undefined
}
#4
-3
Solved the problem by writing a wrapper function that is called to load the controllers and stuff and at such I'm able to tell when each directive is loaded.
通过编写一个包装函数来加载控制器之类的东西来解决这个问题,这样我就可以知道何时加载每个指令。