title: How to encapsulate single and temporal events in a service?
body: <p>I'm trying to encapsulate the events in a service in order to implement a mechanics to subscribe / unsubscribe the listeners when a controller's scope is destroyed. This because I have been using the <code>rootScope.$on</code> in the following way:</p>
  <pre><code>if(!$rootScope.$$listeners['event']) {
      $rootScope.$on('event', function(ev, data){
          // do some...
      });
  }
  </code></pre>
  <p>or</p>
  <pre><code>$scope.$on('$destroy', function(ev, data){
      // unsubscribe the listener
  });
  </code></pre>
  <p>So I just need one listener of this event, I need to delete the existing listener when the controller is no longer alive, because the function I registered earlier is still being triggered.</p>
  <p>So I need to implement a <code>$destroy</code> event listener on my controller, to destroy the listener when the scope is destroyed, but I don't want to do that code each time I create an event.
  That's why I want to create a service in where I'm going to encapsulate the events.</p>
  <pre><code>angular.module('core').factory('event', [
      function() {
          var service = {};
          service.events = {};
          service.on = function(scope, eventId, callback) {
              scope.$on('$destroy', function(ev, other){
                  //unsubscribe
              });
              service.events[eventId] = callback;
              // scope = null; I guess ?
          };
          service.emit = function(eventId, data){
              if (service.events[eventId])
                  service.events[eventId](data);
              else
                  return new Error('The event is not subscribed');
          };
          return service;
      }
  ]);
  </code></pre>
  <p>This could be done using <strong>$rootScope</strong> instead of my own methods but encapsulating the <code>$on</code> and <code>$emit</code> of <strong>$rootScope</strong>, but at the end I'll have the same issue here.</p>
  <p>So these are my questions:</p>
  <ol>
  <li>Is a good practice to pass the scope ref value to a service?</li>
  <li>What is the meaning of <code>$$destroyed</code>? when this is true means that angularJS has no internal references to the instance?</li>
  <li>Should I do a <strong>scope</strong> = null in my service to let GC delete the object or does angularJS handle an explicit delete?</li>
  <li>Is there a better way to do what I want?</li>
  </ol>