如何在另一模块控制器注入到指令 [英] How to inject a controller to a directive in another module
问题描述
我有一个模块'paramApp与控制器ParamCtrl
I have a module 'paramApp' with controller 'ParamCtrl':
var app = angular.app('paramApp', []);
app.controller('ParamCtrl', ['$scope', '$http', function($scope, $http) {
.
.
.
}]);
我还有一个模块deviceApp'与指令'DeviceDirective
I have another module 'deviceApp' with directive 'DeviceDirective':
var app2 = angular.app('deviceApp', []);
app2.directive('DeviceDirective', function() {
return {
.
.
.
controller: // I want to reference ParamCtrl here
controllerAs: param
};
});
我读的地方,我需要使用angular.injector(或$喷油器)注入控制器,但我不太确定如何使用它。我使用注射器的尝试没有成功。
I read somewhere that I need to use angular.injector (or $injector) to inject the controller, but I'm not so sure how to use it. My attempts to use the injector didn't work.
这是正确的做法?我是否需要申报paramApp'在'deviceApp声明这样的,
Is this approach right? Do I need to declare 'paramApp' in 'deviceApp' declaration like this,
var app2 = angular.app('deviceApp', ['paramApp']);
请有关如何使用注射器在指令控制器引用另一个模块的控制有所帮助。
Please help on how to use injector in the directive controller to reference another module's controller.
推荐答案
我想你的意思 angular.module('paramApp',[])
和 angular.module('deviceApp',[])
?您声明依赖性的方法似乎虽然权。我建议这样你就不会污染全局范围内的所有的模块没有分配给变量。为了您例如,你可以尝试:
I think you mean angular.module('paramApp', [])
and angular.module('deviceApp', [])
? Your approach of declaring the dependency seems right though. I would recommend not assigning all your modules to variables so you aren't polluting the global scope. For your example you could try:
angular.module('paramApp', []);
angular.module('paramApp').controller('ParamCtrl', ['$scope', '$http', function($scope, $http) {
.
.
.
}]);
angular.module('deviceApp', ['paramApp']);
angular.module('deviceApp').directive('DeviceDirective', function () {
return {
.
.
.
controller: 'ParamCtrl'
};
});
这篇关于如何在另一模块控制器注入到指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!