title: Dependencies between angular directives
body: <p>I've created a small app that has 2 directives: One that adds a google map div and initialize it, and the second, which shows layers that contain markers. The directives are independent, but I want them to communicate: The <em>layers</em> directive is independent, yet it needs to use the google maps directive to add markers to the map. I used $broadcast through $rootScope to communicate between the directives.</p>
  
  <p>The directives are defined as follows:</p>
  
  <pre><code>angular.module('googleMapModule', [])
         .directive('myGoogleMap', function(){
           template: '&lt;div id="map" /&gt;',
           controller: function($scope){
               // some initializations
  
               // Listen for event fired when markers are added
               $scope.$on('addMarkersEvent', function(e, data){
                   // do something
               }
           }
   }
  
  
   angular.module('layersDirective', [])
          .directive('myLayers', function() {
             templateUrl: 'someLayersHtml.html',  
             controller: function($http, $scope, $rootScope){
                 // Get layers of markers, etc.
  
                 // On specific layer click, get markers and:
                  $rootScope.broadcast('addMarkersEvent', {
                     data: myMarkers 
                  });
             }
          });
  </code></pre>
  
  <p>After this long prologue here are my questions:</p>
  
  <p>How should the connection between the two directives be implemented? Is it correct to use $rootScope and $broadcast or should there be a dependency between the myLayers directive and the myGoogleMap directive?</p>
  
  <p>Furthermore, I've read about when to use controller, link and compile, yet I don't know the right way to use them here. My guess is that myGoogleMap should define its API in a controller and that myLayers should be dependent on myGoogleMap. </p>
  
  <p>The example I wrote here works fine in my application. I'm looking for guidance on how to do it right and to understand what I did wrong here.</p>
  
  <p>Thanks, Rotem</p>