0

I have a set of two directives that call each other recursively to render a tree structure.

I need to pass a controller function reference to those directives so in turn, they need to pass each other that reference. It seems to work except for the parameters.

Passing the reference to the directive:

<item-list items="data" functionreference="controllerfunction(name)"></item-list>

Passing the reference between directives:

<item-list items="item.children" functionreference="functionreference(name)"></item-list></li>

Calling the function:

$scope.functionreference({name: name});

Here's a example (jsfiddle)

What would be the proper way to declare and pass the function reference?

2
  • What is name in controllerfunction(name) when used in view? Commented Apr 14, 2014 at 15:43
  • I got that from this question: stackoverflow.com/questions/15991137/…. You're supposed to pass the function along with the name of the parameters. Then when you invoke the function, you have to use those names: $scope.functionreference({paramname: value}). It works in this question's example... but not in mine :-( Commented Apr 14, 2014 at 15:49

2 Answers 2

1

In your itemList template:

template: '<ul><item ng-repeat="item in items" item="item" functionreference="functionreference(name)'

you reference the function functionreference(name), but you can't call that (you should be calling functionreference({name:name}) instead.

So, changing your template to:

tempate: '<ul><item ng-repeat="item in items" item="item" functionreference="functionreference({name: name})'

it should work fine.

UPDATE:
The same holds for the appended <li> in the item directive:

<item-list items="item.children" functionreference="functionreference({name:name})"></item-list>
// Instead of: functionreference(name)

See, also, this updated demo.

Sign up to request clarification or add additional context in comments.

2 Comments

Updated demo does not work for children when controller function is called. This update has to be done in two places. I should have edited your answer instead. :) @user280767
Indeed (but you get the point) :) I updated the answer and the demo (just in case someone runs into the same problem).
1

You have to make sure to use like this. Make sure the edit is done in two places as shown below.

Use functionreference="functionreference({name:name})" wherever you want to use the controller function inside directives.

myApp.directive('itemList', function ($compile) {
    return {
        restrict: 'E',
        template: '<ul><item ng-repeat="item in items" item="item" ' +  
                  'functionreference="functionreference({name:name})"> ' + 
                  '</item></ul>',
        scope: {
            items: '=',
            functionreference: '&'
        }
    };
});

and

$element.append('<li>{{item.name}} [   ' +
                '<a href="#" ng-click="directivefunction(item.name)">directivefunction</a> / ' + // DOES work
                '<a href="#" ng-click="functionreference({name:item.name})">functionreference</a>]' + // Does NOT work
                            '<item-list items="item.children" functionreference="functionreference({name:name})"></item-list></li>');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.