title: AngularJS - Wrapping directives
body: <p>It seems like I get confused by isolated scopes in directives and hope you can help me out.</p>
  
  <p>I tried to wrap a piece of code (which contains some custom directives) into a new directive to reduce code duplication. Obviously I needed to give some attributes like <code>ng-model</code> into my new directive as a parameter to make the directive reusable. <code>ng-model</code> does not like expressions though (I tried <code>ng-model="{{myVariableWhichContainsDesiredNgModelString}}"</code> at first) and thus I ended up at this article: <a href="https://stackoverflow.com/questions/14115701/angularjs-create-a-directive-that-uses-ng-model">AngularJS - Create a directive that uses ng-model</a>. </p>
  
  <p>While the accepted answer seems to work for a simple setup, I edited the plunker from the accepted answer to test out if it would work with nested directives as well: (in my app I need to wrap directives from a third party-library which I can not edit) <a href="http://plnkr.co/edit/0soT4nLkFqob4lHxyJ1Y?p=preview" rel="nofollow noreferrer">Plunker</a>. In my code each directive seems to generate its own scope and two-way-databinding by using <code>=</code> in the scope definition does not seem to work out as desired.</p>
  
  <p><strong>EDIT:</strong> Since it was not clear what i am asking I edited the Plunker above and will rephrase the question: In the Plunker I have three input-fields which are supposed to bind to the same model-value. This works initially, but as soon as I edit the third input-field it generates its own variable in its isolated scope instead of updating the initial value. Obviously the third input field refers to the new variable from that point on. How can I avoid that behaviour and keep the input linked to <code>$scope.model.name</code>?</p>
  
  <p><strong>Observation</strong>: removing the isolated-scope-directive from the template makes everything work as expected...</p>
  
  <pre><code>template: '&lt;div&gt;&lt;my-input ng-model="myDirectiveVar"&gt;&lt;/my-input&gt;&lt;/div&gt;',
  </code></pre>
  
  <p>instead of </p>
  
  <pre><code>template: '&lt;div&gt;&lt;my-isolated-scope-directive&gt;&lt;my-input ng-model="myDirectiveVar"&gt;&lt;/my-input&gt;&lt;/my-isolated-scope-directive&gt;&lt;/div&gt;',
  </code></pre>
  
  <p><a href="http://plnkr.co/edit/0soT4nLkFqob4lHxyJ1Y?p=preview" rel="nofollow noreferrer">Plunker</a></p>
  
  <p><strong>HTML:</strong></p>
  
  <pre><code>&lt;!-- this binds to the model which i would like all my inputs to bind to.--&gt;
  &lt;input ng-model="name"&gt;
  
  &lt;!-- Example 1: This seems to generate a new modelvalue in the isolated-scope directive. Can I avoid this without modifying that directive?--&gt;
  &lt;my-isolated-scope-directive&gt;&lt;my-input ng-model="name"&gt;&lt;/my-input&gt;&lt;/my-isolated-scope-directive&gt;
  
  &lt;!-- Example 2: This is what i would like my code to look like in the end: One directive which uses the code-snippet of Example 1 as template and passes some parameters into that template.--&gt;
  &lt;my-wrapper-directive my-directive-var="name"&gt;&lt;/my-wrapper-directive&gt;
  </code></pre>
  
  <p><strong>Directives:</strong></p>
  
  <p><code>my-input</code> contains a modified input-field:</p>
  
  <pre><code>app.directive('myInput', function() {
      return {
          restrict: 'E',
          replace: true,
          require: 'ngModel',
          template: '&lt;input class="some"&gt;',
          link: function($scope, elem, attr, ctrl) {
              console.debug($scope);
          }
      };
  })
  </code></pre>
  
  <p><code>my-isolated-scope-directive</code> is a placeholder-directive with its own isolated scope to simulate the behaviour for nested directives:</p>
  
  <pre><code>.directive('myIsolatedScopeDirective', function() {
      return {
          restrict: 'E',
          transclude: true,
          replace: true,
          scope: {
              something: '='
          },
          template: '&lt;div ng-transclude&gt;&lt;/div&gt;',
          link: function($scope, elem, attr, ctrl) {
              console.debug($scope);
          }
      };
  })
  </code></pre>
  
  <p><code>my-wrapper-directive</code> encapsulates both previous directives and accepts a parameter which should be used as ng-model value of the input field:</p>
  
  <pre><code>.directive('myWrapperDirective', function() {
      return {
          restrict: 'E',
          transclude: false,
          replace: true,
          scope: {
            myDirectiveVar: '='
          },
          template: '&lt;div&gt;&lt;my-isolated-scope-directive&gt;&lt;my-input ng-model="myDirectiveVar"&gt;&lt;/my-input&gt;&lt;/my-isolated-scope-directive&gt;&lt;/div&gt;',
          link: function($scope, elem, attr, ctrl) {
              console.debug($scope);
          }
      };
  });
  </code></pre>
  
  <p>Any suggestions and hints on what I am missing are appreciated. Can I maybe somehow link <code>ng-model</code> to a service-instance without making my directive dependant on that service?</p>