在另一个控制器中调用一个控制器的方法angularjs

Call method of an controller in another controller angularjs

我尝试在CustomerController中调用一个civilitiyController的方法。因此,通过我的搜索,我找到了事件的管理器来调用方法,但我没有成功 return 从 CivilityController 到 CustomerController 的结果。

我已经试过了:

1/ 文明控制器:

$scope.$on("getListCivilities", function(event, args){
     $scope.civilities = getCivilitiesList();
});

customersController :

$scope.$broadcast("getListCivilities");
console.dir($scope.civilities) // after run civilities = undefined

2/CivilitiesController:

$scope.$on("getListCivilities" , function(event, args){
     var list = getCivilitiesList();
     return list;
});

客户控制器:

$scope.civilities = $scope.$broadcast("getListCivilities");
console.dir($scope.civilities); //display var of broadcast

3/ 编辑: 在第一个答案之后,我尝试了这个:

礼仪管理员:

 function getCivilitiesList()
    {
        var reqGetCivilities = $http({ url: 'api/Civilities/Get' });
        reqGetCivilities.success(function(data){
            $scope.civilities = data;
            $scope.$broadcast("getListCivilities", { list: $scope.civilities });
            return data;
        });
    }
    getCivilitiesList(); 

客户控制器:

function test()
    {
        $scope.$on("getListCivilities", function (event, args) {
            $scope.civilities = args.list;
            console.log('test0');
            console.dir($scope.civilities);
        });
    }

    test();

$scope.$on 从未执行过,我不明白为什么。

希望有人能帮助我。

我想下面应该可行:

function CivilitiesController($scope) 
{
  $scope.$on('someEvent', function(event, args) {});
  // another controller or even directive
}

function CustomersController($scope) 
{
  $scope.$emit('someEvent', args);
}

JSFiddle(更多细节):http://jsfiddle.net/nikdtu/moo89kaw/

检查这个plunker

app.controller('Controller1', function($scope) {
  $scope.name = 'World';
  $scope.$on('ValueUpdated', function(event, args) {
    $scope.name = args.currentValue;
  });
});

app.controller('Controller2', ['$rootScope', '$scope', function($rootScope, $scope) {
  $scope.myData = "type here";
  $scope.broadCast = function() {
    $rootScope.$broadcast('ValueUpdated', {
      currentValue: $scope.myData
    });
  }

我使用了广播,但你也可以使用服务和观察者来做到这一点。

Ahmet Zeytindalı,这是我的整个 CivilitiesController:

(function () {
'use strict';
'use strict';

angular
    .module('LSapp')
    .controller('CivilitiesCtrl', CivilitiesCtrl)

CivilitiesCtrl.$inject = ['$scope', '$http', '$rootScope'];

function CivilitiesCtrl($scope, $http, $rootScope) {
    function getCivilitiesList()
    {
        var reqGetCivilities = $http({ url: 'api/Civilities/Get' });
        reqGetCivilities.success(function(data){
            $scope.civilities = data;
        });
    }
    getCivilitiesList();

    function getList()
    {
        $rootScope.$broadcast("getListCivilities", { list: $scope.civilities });
    }
    getList();
}

})();

以及检索列表的方法:

(function () {
'use strict';

angular
    .module('LSapp')
    .controller('CustomersCtrl', CustomersCtrl)

CustomersCtrl.$inject = ['$scope', '$http', '$location', '$modal', '$window', '$compile','$cookies', '$state','locker','$q','$timeout', '$rootScope'];

function CustomersCtrl($scope, $http, $location, $modal, $window, $compile, $cookies, $state, locker, $q, $timeout) {
     //some code
 function test()
    {
        $scope.$on("getListCivilities", function (event, args) {
            $scope.civilities = args.list;
            console.log('$on args : ');
            console.dir(args);
        });
    }
    test();

}

});

$on 方法没有 运行,如果我在方法后面加上 console.log($scope.civilities),结果总是不确定的。