Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to pass a function reference in a recursive directive in Angular?
    primarykey
    data
    text
    <p>I have this directive:</p> <pre><code>app.directive('recursiveListItem', ['$http', 'RecursionHelper', function ($http, RecursionHelper) { return { restrict: 'E', scope: { parent: '=', onNodeClick: '&amp;', }, compile: function (element, attributes) { return RecursionHelper.compile(element); }, template: '&lt;div class="list-group-item-heading text-muted parent "&gt; \ &lt;input type="checkbox" data-ng-click="visible = !visible" id="{{parent.Name}}"&gt;\ &lt;label for="{{parent.Name}}"&gt;&amp;nbsp;&amp;nbsp;&lt;/label&gt;\ &lt;a href="javascript:void(0)" data-ng-click="onNodeClick({node: parent})"&gt;{{parent.Name}}&lt;/a&gt; \ &lt;/div&gt; \ &lt;ul data-ng-if="parent.Children.length &gt; 0" data-ng-show="visible"&gt; \ &lt;li ng-repeat="child in parent.Children"&gt; \ &lt;recursive-list-item data-parent="child" data-on-node-click="onNodeClick"&gt;&lt;/recursive-list-item&gt; \ &lt;/li&gt; \ &lt;/ul&gt;', }; }]); </code></pre> <p>and here is the helper:</p> <pre><code>app.factory('RecursionHelper', ['$compile', function ($compile) { var RecursionHelper = { compile: function (element) { var contents = element.contents().remove(); var compiledContents; return function (scope, element) { if (!compiledContents) { compiledContents = $compile(contents); } compiledContents(scope, function (clone) { element.append(clone); }); }; } }; return RecursionHelper; }]); </code></pre> <p>Everything works like a charm, but I don't get my <code>on-node-click</code> to work. For the all the root items the 'callback' works fine, but from there on, all the children won't fire the callback. I think it has something to do with passing the function reference to the next child in the template.</p> <p>I've also tried <code>data-on-node-click="onNodeClick(node)"</code>, but that doesn't work either.</p> <p>Does someone know how to pass the function reference to the child nodes?</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