如何从另一个模块调用函数 [英] How to call a function from another module
本文介绍了如何从另一个模块调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的angularJS应用程序中,我有两个模块:模块A和模块B.
In my angularJS application, I have two modules : module A and module B.
angular.module('A').controller('ACtrl',function($scope){
$scope.alertA = function(){alert('a');}
//...
});
angular.module('B').controller('BCtrl',function($scope){
//...
});
如何在模块B中调用函数alertA
?
How to call the function alertA
in the module B ?
推荐答案
您需要在模块A 中定义工厂:
var moduleA= angular.module('A',[]);
moduleA.factory('factoryA', function() {
return {
alertA: function() {
alert('a');
}
};
});
然后使用模块B 中的alertA
工厂:
angular.module('B',['A']).controller('BCtrl',function($scope,'factoryA'){
factoryA.alertA();
});
这篇关于如何从另一个模块调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文