2017-12-14 34 views
0

コントローラから別のコントローラに関数を呼び出すのに数時間をかけています。Angularjsがコントローラから別のコントローラに関数を呼び出す

私は同様のトピック(私が試したが、私は確かにあまりにも悪いんだ)見た:私が探しているよう What's the correct way to communicate between controllers in AngularJS? Can one controller call another?

しかし、具体的な例を。

コントローラ:

app.controller('Controller1', function($scope, $modal, $modalInstance, job, JobServices) { 

    $scope.job1 = function() { 
     JobServices.job1($scope.name) 
     .success(function(data, status, headers, config) { 
      // something 
     }); 
    } 


    function doSomething(){ 
     // How call test() from controller2 ? 
    } 
} 

app.controller('Controller2', function($scope){ 
    function test(){ 
     do a lot of things 
    } 
} 

それを行うための最善かつ最も簡単な方法は何ですか?

はあなたにたくさん

EDITありがとう:

私はすでにそのような何か使用してみました:

app.controller('Controller1', ['$scope', '$rootScope', function($scope, $rootScope, $modal, $modalInstance, job, JobServices) { 

をしかし、私はjob1が未定義であるというエラーを持っている...

+0

あなたのロジックを[service/factory](https://stackoverflow.com/a/28337130/8495123)に入れ、代わりにその関数を呼び出したいと思うかもしれません。 –

+0

[$ onと$ broadcast in angular](https://stackoverflow.com/questions/19446755/on-and-broadcast-in-angular) – Sajal

+0

私の問題は、私が関数を使用すると多くのエラーが発生していることですjob1のような私の関数から、 '$ scope'、 '$ rootscope'、function($ scope、$ rootscope、$ modal、$ modalInstance、job、JobServices)へのスコープ($ scope、$ modal、$ modalInstance、job、JobServices)私の編集を参照してください) –

答えて

0

あなたの可能性$ scopeと$ scope。$ onのような小さなビットです。

app.controller('Controller1', ['$scope', '$rootScope' function($scope) { 
    function doSomething(){ 
     $rootScope.$emit("callController2", {}); 
    } 
} 

app.controller('Controller2', ['$scope', '$rootScope' function($scope) { 
    $rootScope.$on("callController2", function(){ 
    this.test(); 
    }); 
    function test(){ 
    do a lot of things 
     } 
} 
+0

私が上を言ったように、私はすでにそれを試みましたが、['$ scope'、 '$ rootScope' ...(私の編集を参照してください) –

関連する問題