2

When to use $scope.$on and $scope.emit on angular? I want to learn more about the proper usage of this functions in angular versus using Event Aggregates.

Wesley
  • 4,084
  • 6
  • 37
  • 60
Erick Caccam
  • 91
  • 1
  • 1
  • 2

1 Answers1

17

AgularJS provides $on, $emit, and $broadcast services for event-based communication between controllers.

$emit

It dispatches an event name upwards through the scope hierarchy and notify to the registered $rootScope.Scope listeners. The event life cycle starts at the scope on which $emit was called. The event traverses upwards toward the root scope and calls all registered listeners along the way. The event will stop propagating if one of the listeners cancels it. enter image description here

<!DOCTYPE html>
<html>
   <head>
      <title>Broadcasting</title>
      <script src="lib/angular.js"></script>
      <script>
         var app = angular.module("app", []);

         app.controller("firstCtrl", function($scope) {
           $scope.$on("eventName", function(event, args) {
             $scope.message = args.message;
             console.log($scope.message);
           });
         });

         app.controller("secondCtrl", function($scope) {
           $scope.handleClick = function(msg) {
             $scope.$emit("eventName", { message: msg });
           };
         });
      </script>
   </head>
   <body ng-app="app">
      <div
         ng-controller="firstCtrl"
         style="border:2px solid #E75D5C; padding:5px;"
         >
         <h1>Parent Controller</h1>
         <p>Emit Message : {{ message }}</p>
         <br />
         <div
            ng-controller="secondCtrl"
            style="border:2px solid #428bca;padding:5px;"
            >
            <h1>Child Controller</h1>
            <input ng-model="msg" />
            <button ng-click="handleClick(msg);">Emit</button>
         </div>
      </div>
   </body>
</html>

How it works..

enter image description here

$broadcast

It dispatches an event name downwards to all child scopes (and their children) and notify to the registered $rootScope.Scope listeners. The event life cycle starts at the scope on which $broadcast was called. All listeners for the event on this scope get notified. Afterwards, the event traverses downwards toward the child scopes and calls all registered listeners along the way. The event cannot be canceled.

<!DOCTYPE html>
<html>
   <head>
      <title>Broadcasting</title>
      <script src="lib/angular.js"></script>
      <script>
         var app = angular.module('app', []);

         app.controller("firstCtrl", function ($scope) {
             $scope.handleClick = function (msg) {
             $scope.$broadcast('eventName', { message: msg });
           };
         });

         app.controller("secondCtrl", function ($scope) {
             $scope.$on('eventName', function (event, args) {
             $scope.message = args.message;
             console.log($scope.message);
           });
         });

      </script>
   </head>
   <body ng-app="app">
      <div ng-controller="firstCtrl" style="border:2px solid #E75D5C; padding:5px;">
         <h1>Parent Controller</h1>
         <input ng-model="msg">
         <button ng-click="handleClick(msg);">Broadcast</button>
         <br /><br />
         <div ng-controller="secondCtrl" style="border:2px solid #428bca;padding:5px;">
            <h1>Child Controller</h1>
            <p>Broadcast Message : {{message}}</p>
         </div>
      </div>
   </body>
</html>

How it works..

enter image description here

$on

It listen on events of a given type. It can catch the event dispatched by $broadcast and $emit.

Note

  • If there is no parent-child relation between your scopes you can inject $rootScope into the controller and broadcast the event to all child scopes but you cannot emit your event.

    • You can emit your event only when you have parent-child relation and event propagation is initiated by child. However, $emit can fire an event only for all $rootScope.$on listeners.
Alex Min
  • 11
  • 3
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234