Why do we use $rootScope.$broadcast in AngularJS?

$rootScope.$broadcast is used to broadcast a “global” event which can be caught by any listener of that particular scope. The descendant scopes can catch and handle this event by using $scope.$on.

Syntax:

$rootScope.$broadcast(name, args)

$scope.$on(name, listener);

where listener specifies the function to call when the event is caught.

Approach:

  • Create a ParentController from which you would want to raise/broadcast an event.
  • Use $rootScope.$broadcast in AngularJS to broadcast the event from the ParentController.
  • Create a ChildController or an ExternalController (i.e., not a direct descendant of the ParentController) to catch and handle the event.
  • Use $scope.$on in AngularJS to catch the respective event.

Example: This program uses $rootScope.$broadcast to raise an event using the approach discussed above.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <script src=
  </script>
    <script type="text/javascript">
        var app = angular.module('app', []);
        app.controller('ParentController', function(
                       $rootScope, $scope) {
            $scope.broadcastMessage = function() {
                $rootScope.$broadcast('newEvent', {
                    message: $scope.parentMessage
                });
            };
  
        });
        app.controller('ChildController', function($scope) {
            $scope.$on('newEvent', function(event, args) {
                $scope.message = args.message;
            });
        });
        app.controller('ExternalController', function($scope) {
            $scope.$on('newEvent', function(event, args) {
                $scope.message = args.message;
            });
        });
    </script>
    <style type="text/css">
        h1,
        code {
            color: green;
        }
          
        p {
            color: green;
            display: inline-block;
        }
          
        div {
            border-color: black;
            border-style: solid;
            padding: 10px;
        }
    </style>
</head>
  
<body ng-app="app">
    <div ng-controller="ParentController">
        <h1>Parent Controller</h1>
        <input ng-model="parentMessage">
        <button ng-click="broadcastMessage()">
          Broadcast Message
      </button>
  
        <br>
        <br>
        <div ng-controller="ChildController">
            <h1>Child Controller</h1>
            <p>Message :</p>
  
            <code>{{message}}</code>
        </div>
    </div>
    <br>
    <br>
    <div ng-controller="ExternalController">
        <h1>External Controller</h1>
        <p>Message :</p>
  
        <code>{{message}}</code>
    </div>
</body>
  
</html>

chevron_right


Output:




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.