0

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

Community
  • 1
  • 1
user3821206
  • 609
  • 1
  • 9
  • 22

1 Answers1

1

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);

});
Rahul Arora
  • 4,503
  • 1
  • 16
  • 24
  • thanks Sir for your reply,I tried your code but still not working,the method in the second controller is not called :( – user3821206 Jul 24 '16 at 19:03
  • 1
    Is the second controller active at that time? – Rahul Arora Jul 24 '16 at 19:04
  • how do I know if it's active or not??in my app each view has a controller,I try with Controller1 to display view2 by calling a method in its controller2 Sir – user3821206 Jul 24 '16 at 19:13
  • 1
    you can check if both the controllers are active at the same time by doing console.log("active") in both controllers. As they load you should see two console.log statements in browser console – Rahul Arora Jul 24 '16 at 19:14
  • No Sir,they are not activated at the same time,in this case should I use a service to share Data between those two controllers?? – user3821206 Jul 24 '16 at 19:18