2

If I have a function like this in a directive:

 scope: { loadChildren: "&" }, 

How can I call this from my controller and pass parameters?

$scope.loadChildren(param1, param2); 

My directive executes the function I pass it but I don't see any parameters.

Is there any way to pass them like this?

EDIT re: rodyhaddad's answer

rodyhaddad points the way and gives the correct format for passing parameters to a bound function. However, in order to get it working I also had to add the parameter keys into my HTML like this:

<example load-children="load(param1, param2)"></example> 

Then on my controller I can see parameters:

$scope.load = function (param1, param2) { debugger; }; 
0

1 Answer 1

3

When you do

 loadChildren: "&" 

This is the code that generates the function (source code)

case '&': { parentGet = $parse(attrs[attrName]); scope[scopeName] = function(locals) { return parentGet(parentScope, locals); }; break; 

So as you can see, whatever you pass to loadChildren will become the locals passed to $parse.

So the correct way to call it would be:

$scope.loadChildren({ param1: param1, param2: param2 }); 

If you want another example, here's how event directives (like ngClick) expose $event to their expressions: source code

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

2 Comments

In order to get this to work I also had to add the parameters in the HTML - see my edit.
What about when the parameter is ALSO a function?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.