Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Mixing isolated scope with ngModel is a documented issue, see the <strong>Isolated Scope Pitfall</strong> section in the <a href="http://docs.angularjs.org/api/ng.directive%3angModel.NgModelController">documentation</a>: </p> <blockquote> <p>Isolated Scope Pitfall Note that if you have a directive with an isolated scope, you cannot require ngModel since the model value will be looked up on the isolated scope rather than the outer scope. When the directive updates the model value, calling ngModel.$setViewValue() the property on the outer scope will not be updated. However you can get around this by using $parent.</p> </blockquote> <p>Using this knowledge and some freaky scope experiments I've come with two options that does what you want to do:</p> <p>(1) See this <a href="http://jsfiddle.net/W8cCm/4/">fiddle</a> It makes use of the $parent method as described above.</p> <pre><code>&lt;div ng-controller="MyCtrl"&gt; &lt;div&gt; &lt;input ng-form type="text" ng-model="$parent.search" append-me terms="myTerms"&gt; &lt;/div&gt; {{search}} &lt;/div&gt; myApp.directive('appendMe', ['$compile', function($compile) { return { restrict: 'A', scope: {terms: '='}, link: function(scope, element, attributes) { // linking function console.log(scope.terms); var template = '&lt;p&gt;test&lt;/p&gt;' + '&lt;ul&gt;&lt;li ng-repeat="term in terms"&gt;{{term}}&lt;/li&gt;&lt;/ul&gt;' + '&lt;p&gt;hm…&lt;/p&gt;' element.after($compile(template)(scope)); } } }]); </code></pre> <p>(2) See this <a href="http://jsfiddle.net/W8cCm/3/">fiddle</a> It does not use $parent, instead it uses the isolated scope to publish the search model as configured via ngModel.</p> <pre><code>&lt;div ng-controller="MyCtrl"&gt; &lt;div&gt; &lt;input ng-form type="text" ng-model="search" append-me terms="myTerms"&gt; &lt;/div&gt; {{search}} &lt;/div&gt; myApp.directive('appendMe', ['$compile', function($compile) { return { restrict: 'A', scope: {terms: '=', search: '=ngModel'}, link: function(scope, element, attributes) { // linking function console.log(scope.terms); var template = '&lt;p&gt;test&lt;/p&gt;' + '&lt;ul&gt;&lt;li ng-repeat="term in terms"&gt;{{term}}&lt;/li&gt;&lt;/ul&gt;' + '&lt;p&gt;hm…&lt;/p&gt;' element.after($compile(template)(scope)); } } }]); </code></pre> <p>Both options seem to work just fine.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload