我怎么能调用一个函数是在另一个CONTROLER(角JS) [英] How can i call a function that is in another controler (Angular js)

查看:436
本文介绍了我怎么能调用一个函数是在另一个CONTROLER(角JS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ControllerOne

in ControllerOne

$scope.displayReport = function( status ){

   statusCkeck( status ) ;
}

statusCkeck(); 是另一个控制器-controllerTwo

statusCkeck() ; is in another controller -controllerTwo

我如何从一个控制器调用这个函数来另一个

How can i call this function from one controller to another

我试过 angular.element(的document.getElementById('controllerTwo'))范围()statusCheck();

但它抛出的错误。

请提示

推荐答案

您应该使用服务,而不是为了避免弄脏了code,在其他控制器调用的函数的控制器。如果您分享的功能,只是在服务定义它们,并在需要注入该服务。

You should use a service instead to avoid muddying up the controllers with code that calls functions in other controllers. If you're sharing functions, just define them in a service and inject that service where needed.

angular.module('myApp', [])
  .factory('myService', function() {
    return {
      sharedFunction: function (foo, bar) {
        return foo + bar;
      }
    }
};

这篇关于我怎么能调用一个函数是在另一个CONTROLER(角JS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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