如何从另一个模块调用函数 [英] How to call a function from another module

查看:28
本文介绍了如何从另一个模块调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 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中定义一个工厂:

You need to define a factory in module 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆