Best way for communication between directives
<p>Copied from <a href="https://softwareengineering.stackexchange.com/questions/223006/communication-between-nested-directives">here</a>. May be, I can get more proper answer here!</p>

<p>There seem to be quite a few ways of communicating between directives. Say you have nested directives, where the inner directives must communicate something to the outer (e.g. it's been chosen by the user).</p>

<pre><code>&lt;outer&gt;
  &lt;inner&gt;&lt;/inner&gt;
  &lt;inner&gt;&lt;/inner&gt;
&lt;/outer&gt;
</code></pre>

<p>So far I have 5 ways of doing this</p>

<p><strong>require: parent directive</strong></p>

<p>The inner directive can require the outer directive, which can expose some method on its controller. So in the inner definition</p>

<pre><code>require: '^outer',
link: function(scope, iElement, iAttrs, outerController) {
   // This can be passed to ng-click in the template
   $scope.chosen = function() {
     outerController.chosen(something);
   }
}
</code></pre>

<p>And in the outer directive's controller:</p>

<pre><code>controller: function($scope) {
   this.chosen = function(something) {
   }
}
</code></pre>

<p><strong>$emit event</strong></p>

<p>The inner directive can $emit an event, which the outer directive can respond to, via $on. So in the inner directive's controller:</p>

<pre><code>controller: function($scope) {
  $scope.chosen = function() {
    $scope.$emit('inner::chosen', something);
  }
}
</code></pre>

<p>and in the outer directives controller:</p>

<pre><code>controller: function($scope) {
  $scope.$on('inner::chosen, function(e, data) {
  }
}
</code></pre>

<p><strong>Execute expression in parent scope, via &amp;</strong></p>

<p>The item can bind to an expression in the parent scope, and execute it at an appropriate point. The HTML would be like:</p>

<pre><code>&lt;outer&gt;
  &lt;inner inner-choose="functionOnOuter(item)"&gt;&lt;/inner&gt;
  &lt;inner inner-choose="functionOnOuter(item)"&gt;&lt;/inner&gt;
&lt;/outer&gt;
</code></pre>

<p>So the inner controller has an 'innerChoose' function it can call</p>

<pre><code>scope: {
  'innerChoose': '&amp;'
},
controller: function() {
  $scope.click = function() {
    $scope.innerChoose({item:something});
  }
}
</code></pre>

<p>which would call (in this case) the 'functionOnOuter' function on the outer directive's scope:</p>

<pre><code>controller: function($scope) {
  $scope.functionOnOuter = function(item) {
  }
}
</code></pre>

<p><strong>Scope inheritance on non-isolated scope</strong></p>

<p>Given that these are nested controllers, scope inheritance can be at work, and the inner directive can just call any functions in the scope chain, as long as it doesn't have an isolated scope). So in the inner directive:</p>

<pre><code>// scope: anything but a hash {}
controller: function() {
  $scope.click = function() {
    $scope.functionOnOuter(something);
  }
}
</code></pre>

<p>And in the outer directive:</p>

<pre><code>controller: function($scope) {
  $scope.functionOnOuter = function(item) {
  }
}
</code></pre>

<p>By service injected into both inner and outer</p>

<p>A service can be injected into both directives, so they can have direct access to the same object, or call functions to notify the service, and maybe even register themselves to be notified, in a pub/sub system. This doesn't require the directives to be nested.</p>

<p>Question: What are any potential drawbacks and advantages of each over the others?</p>