BroadCast & Emit Example

this is the example for the broadcast and emit

by DineshAngappa

HTML

<div ng-app="myModule" ng-controller="eventCtrl">
    <p>This is the Parent Controller Count :: {{count}} </p>
    <div ng-controller="eventCtrl">
        
      <button ng-click="$emit('MyEvent')">$emit</button>
      <button ng-click="$broadcast('MyEvent')">$broadcast</button>
        <p> this is the middle one Count :: {{count}} </p>
       
        
        <div ng-controller="eventCtrl">
            <p>this is the child controller Count :: {{count}}</p>
    </div>
    </div>

    
    
</div>

JavaScript

var app = angular.module('myModule', []);

app.controller('eventCtrl', ['$scope', function($scope){
  $scope.count = 0;
    
  $scope.$on('MyEvent', function() {
    $scope.count++;
  });
}]);