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

4
  • 1
    Are your both controllers on the same view ? Commented Jul 24, 2016 at 18:47
  • no MiTa they are not on the same view :) Commented Jul 24, 2016 at 18:48
  • 1
    So it's not possible to do it with events, controller will listen only if it's active - it's assigned to a page element. Commented Jul 24, 2016 at 18:59
  • ok thanks MiTa,in this case should I use a service to share Data between those two controllers?? Commented Jul 24, 2016 at 19:21

1 Answer 1

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

});
Sign up to request clarification or add additional context in comments.

5 Comments

thanks Sir for your reply,I tried your code but still not working,the method in the second controller is not called :(
Is the second controller active at that time?
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
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
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??

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.