title: global communication in angular module: event bus or mediator pattern/service
body: <p>So far I have seen many solutions of the problem. The simplest one is, of course, to <code>$emit</code> an event in <code>$rootScope</code> as an event bus e.g. ( <a href="https://github.com/btilford/anti-patterns/blob/master/angular/Angular.md" rel="nofollow noreferrer">https://github.com/btilford/anti-patterns/blob/master/angular/Angular.md</a> ) </p>
  
  <pre><code>angular.module('myModule').directive('directiveA', function($rootScope) {
    return {
      link : function($scope, $element) {
        $element.on('click', function(event) {
          $rootScope.$emit('directiveA:clicked', event);
        });
      }
    }
  });
  angular.module('myModule').directive('directiveB', function() {
    return {
      link : function($scope, $element) {
        $rootScope.on('directiveA:clicked', function(event) {
          console.log('received click event from directiveA');
        });
      }
    }
  });
  </code></pre>
  
  <p>and another one is to declare a service with a mediator or pubsub functionality / an enclosed scope e.g. ( <a href="https://stackoverflow.com/questions/22550079/communicating-between-a-multiple-controllers-and-a-directive">Communicating between a Multiple Controllers and a directive</a>. ) </p>
  
  <pre><code>module.factory('MessageService',
    function() {
      var MessageService = {};
  
      var listeners = {};
      var count = 0;
      MessageService.registerListener = function(listener) {
        listeners[count] = listener;
        count++;
  
        return (function(currentCount) {
          return function() {
            delete listeners[currentCount];
          }
        })(count);
      }
  
      MessageService.broadcastMessage = function(message) {
        var keys = Object.keys(listeners);
  
        for (var i = 0; i &lt; keys.length; i++) {
          listeners[keys[i]](message);
        }
      }
  
      return MessageService;
    }
  );
  </code></pre>
  
  <p>The question are: </p>
  
  <ul>
  <li>is there point to use the second one in an angular application? </li>
  <li>and what are pros and cons of each of those in comparison to each other?</li>
  </ul>