Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to refresh controller from a directive?
    primarykey
    data
    text
    <p>Here is the demo : <a href="http://plnkr.co/edit/atZZXM6YVYQL0SilpWxq?p=preview" rel="nofollow">http://plnkr.co/edit/atZZXM6YVYQL0SilpWxq?p=preview</a></p> <p>In a table, I display each value of my collection, everything is fine. But when I remove one element in the list, the controller isn't refreshed. I choose a directive in order to add and remove an edit line after each element. I needed <code>jQuery</code> for that but maybe there is a more elegant way to achieve it. I suppose a <code>$watch</code> or <code>$broadcast</code> is missing.</p> <h2>HTML</h2> <pre><code>&lt;table&gt; &lt;tr ng-repeat="item in items" item-directive="item"&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Total&lt;/td&gt; &lt;td&gt;{{getTotal()}}&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; </code></pre> <h2>AngularJS</h2> <pre><code>app.directive("itemDirective", function($compile){ var trLine = '&lt;tr&gt;&lt;td&gt;{{itemDirective.name}}&lt;/td&gt;&lt;td&gt;{{itemDirective.value}}&lt;/td&gt;' +'&lt;td&gt;&lt;button ng-click="edit()"&gt;Edit&lt;/button&gt;&lt;/td&gt;' +'&lt;td&gt;&lt;button ng-click="remove()"&gt;Remove&lt;/button&gt;&lt;/td&gt;' +'&lt;td&gt;&lt;button ng-click="removeFromController()"&gt;Remove from controller&lt;/button&gt;&lt;/td&gt;' +'&lt;/tr&gt;'; var trEditLine = '&lt;tr class="editLine"&gt;&lt;td style="background: #E6E6E6"&gt;&lt;div&gt;' +'&lt;button ng-click="done();"&gt;Done&lt;/button&gt;' +'&lt;button ng-click="cancel();"&gt;Cancel&lt;/button&gt;&lt;/div&gt;&lt;/td&gt;' +'&lt;/tr&gt;'; return { template: trLine, replace: false, scope: { itemDirective: '=' }, link: function(scope, element, attrs) { scope.edit = function() { if(!element.edited) { element.edited = true; element.after($compile(trEditLine)(scope)); } }; scope.cancel = function() { element.edited = false; $(".editLine").remove(); }; scope.done = function() { element.edited = false; $(".editLine").remove(); }; scope.remove = function() { element.remove(); }; } }; }); </code></pre> <p>This will remove the line as expected but the total doesn't change. </p> <p>Any help is welcome.</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.
 

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