title: delegating function calls between controllers in angularjs
body: <p>Following situation:</p>
  
  <p>There are two directives with controllers (A &amp; B) - which are both children of another controller (C).</p>
  
  <p>Controller A manages stuff for its model. 
  Now Controller C needs to call functions from Controller A to modify some stuff. And furthermore it needs access to some properties and read them.</p>
  
  <p>I'm uncertain on what the right approach to communication is. And wheter to stick to one approach.</p>
  
  <p>Following up is a small code example to illustrate the problem in a more concrete way.</p>
  
  <p>First off there is a provider where components can register themselfes. </p>
  
  <pre><code>angular.module('components', [])
         .provider('db', function(){
           this.registerComponent = function(name, component){
             ...
           }
         });
  </code></pre>
  
  <p>Now there is a directive &amp; a Controller (A) which displays concrete components.</p>
  
  <pre><code>angular.module('components')
         .directive('componentDashboard', function(){
         return {
           scope:{
             concreteComponents: '='
           },
           controller: function($scope){
             $scope.model = concreteComponents;
             $scope.model.someImportantProp = "foo";
             $scope.addComponent = function(c){...}
           }
         }
         }) 
  </code></pre>
  
  <p>That is basically the setup. The directive componentDashboard can display a set of components which registered to the db provider.</p>
  
  <p>There are controller functions (A) like addComponent which needs to be called from outside of the controller (Controller B wants to call this). Furthermore Controller B wants to access different properties and so on.</p>
  
  <p>Whats the preferred way of doing this? </p>
  
  <p>At the moment there are these paradigmas used:</p>
  
  <p>1) Factory hack ?! Basically there is a factory with some functions:</p>
  
  <pre><code>angular.module('components')
          .factory('componentStub', function($log){
            return {
              addComponent : function(c){
                $log.error("stub not overwritten");
              }
            }
          })
  </code></pre>
  
  <p>These functions are now overwritten by the component's directive:</p>
  
  <pre><code>angular.module('components')
     .directive('componentDashboard', function(componentStub){
     return {
       scope:{
         concreteComponents: '='
       },
       controller: function($scope){
         $scope.model = concreteComponents;
         $scope.model.someImportantProp = "foo";
         $scope.addComponent = function(c){...}
         componentStub.addComponent = function(c){
           $scope.addComponent(c);
         }
       }
     }
     })
  </code></pre>
  
  <p>2) event based</p>
  
  <pre><code>angular.module('components')
         .factory('notificationCenter', function(){
           return {
             registerToNotification: function(id, not, cb){..}
           } 
         })
         .directive('componentDashboard', function(notificationCenter){
           return {
             scope:{
               concreteComponents: '='
             },
           controller: function($scope){
             $scope.model = concreteComponents;
             $scope.model.someImportantProp = "foo";
             $scope.addComponent = function(c){...}
             notificationCenter.registerToNotification("foo",     "doAddComponent", function(c){
                $scope.addComponent(c)
              }
           }
         }
         }) 
  </code></pre>
  
  <p>At the moment there are both approaches used. There are some advantages of using this. Its developed fast, there are just few depencencies or restrictions to it. And it works.</p>
  
  <p>But! I'm unsure about whether it is a good approach. I'm experiencing kinda bad maintenance on that and the more complex it gets(lets say some components can be added and some not -> states) the more it feels like not the right approach.</p>
  
  <p>How should this problem be approached ? </p>
  
  <p>Sorry for the long question, thanks in advice</p>
  
  <p>Schemii</p>