Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to stop propagation of & functions into nested directives
    primarykey
    data
    text
    <p>here I got one ctrl</p> <pre><code>controllers.ctrl = function($scope){ $scope.trData = [ {name : 'elem1',children : []}, {name : 'elem2',children : [{name : 'elem3',children : []}] } }; $scope.testFunction = function(tr){ console.debug(tr); } } </code></pre> <p>And a nested directive</p> <pre><code>directives.nested = function($compile){ restrict: 'E', replace: false, scope: { tr: '=', test : '&amp;' }, link: function(scope, element, attrs) { scope.setActive = function(){ console.debug("A"); console.debug(scope.tr); scope.test({tr:scope.tr}); console.debug("B"); }; var template = '&lt;span data-ng-click="setActive();"&gt;{{tr.name}} - &lt;/span&gt;'; if(scope.tr.children.length &gt; 0){ template += '&lt;ul&gt;&lt;li data-ng-repeat="tr0 in tr.children"&gt;'; template += '&lt;nested tr="tr0" test="test(tr)"&gt;&lt;/nested&gt;'; template += '&lt;/li&gt;&lt;/ul&gt;'; } var newElement = angular.element(template); $compile(newElement)(scope); element.replaceWith(newElement); } </code></pre> <p>And in my template, of course : </p> <pre><code>&lt;ul&gt; &lt;li data-ng-repeat="tr in trData"&gt; &lt;nested tr="tr" test="testFunction(tr)"&gt;&lt;/nested&gt; &lt;/li&gt; &lt;/ul&gt; </code></pre> <p>When I click on a elem1, I've got in the console: </p> <pre><code>A {name : 'elem1',children : []} B </code></pre> <p>If I click on 'elem3', I got</p> <pre><code>A {name : 'elem3',children : []} {name : 'elem2',children : [{name : 'elem3',children : []}] } B </code></pre> <p>The function testFunction is called twice. I would like to have it called only once with elem3. </p> <p>I am sure there is a better way to do this.</p> <p>Fiddle from Banana-In-Black : <a href="http://jsfiddle.net/T4uKf/" rel="nofollow">http://jsfiddle.net/T4uKf/</a></p>
    singulars
    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.
 

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