user3821206
user3821206

Reputation: 619

call a method and send value from controller1 to controller2 in AngularJS

based on this reply link I tried to send a data when I click on a button and call a method from Controller1 to Controller2 this is my try:

Controller1:

$scope.DetailsLivraison = function(){                                       
                var idv = $scope.idBonSortie;
       $rootScope.$emit("CallParentMethod", idv);                                                                                                  
}

Controller2:

$rootScope.$on("CallParentMethod", function(){
                   $scope.parentmethod(idv);
                });

$scope.parentmethod = function(idv) {
 //Data traitment
}

my problem is that,the method in the second controller is not called,I have defined $rootscope in both controllers any help please to solve the problem thanks for help

Upvotes: 0

Views: 117

Answers (1)

Rahul Arora
Rahul Arora

Reputation: 4543

Firstly, to make this happen both your controllers should be active at that time.

Secondly, you can use the code below:

$rootScope.$broadcast('CallParentMethod', { //can also use $emit
    idv: idv,
});

At the receiving end in the other controller:

$rootScope.$on('CallParentMethod', function(event, args) {

       $scope.parentmethod(args.idv);

});

Upvotes: 1

Related Questions