0

i am learning angular. i am looking for a example which would show me how many different ways exist by which independent two controller can communicate in each other.

i have found just now how a parent and child controller can communicate in each other. here is my sample code and it is working fine. js fiddle here http://jsfiddle.net/tridip/oa9gc3ew/

the below post really help me to get the idea https://stackoverflow.com/a/14502755/6188148

<div ng-app ng-controller="ParentCtrl">
<button ng-click="ShowParent()">ShowParent</button>

    <div ng-controller="ChildCtrl as vm">
    <button ng-click="ShowChild()">ShowChild</button>
    <br>
    <br>
       {{$parent.cities}}
       <br>
       {{vm.parentcities}}
    </div>
</div>



function ParentCtrl($scope) {
    $scope.cities = ["NY", "Amsterdam", "Barcelona"];
            $scope.ShowParent = function() {
            alert('parent');
       $scope.$broadcast('childEvent', [1,2,3]);
        };  

            $scope.$on('parentEvent', function(event, data) { alert(data); });
}

function ChildCtrl($scope) {
    $scope.parentcities = $scope.$parent.cities;
            $scope.ShowChild = function() {
            alert('child');
      $scope.$emit('parentEvent', [1,2,3]);
        };    

    $scope.$on('childEvent', function(event, mass) 
    { 
        alert(mass); 
    });        
}

show anyone please come with few examples to show how two independent controllers can communicate in each other. show me few different approach people use for communication between controllers. thanks

Community
  • 1
  • 1
Monojit Sarkar
  • 2,353
  • 8
  • 43
  • 94
  • 1
    Possible duplicate of [Invoke one controller from another](http://stackoverflow.com/questions/36663503/invoke-one-controller-from-another) – GG. Apr 18 '16 at 12:56
  • Services. Anyway, controllers should not communicate with each other in well designed application. – dfsq Apr 18 '16 at 12:57
  • Or this one, even better: [Can one controller call another?](http://stackoverflow.com/questions/9293423/can-one-controller-call-another) – GG. Apr 18 '16 at 12:58
  • @dfsq not clear what your are trying to say. are you saying service communicate in each other.....controller not? please guide me in more details. – Monojit Sarkar Apr 18 '16 at 13:10

0 Answers0