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