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;
};
Jon
  • 3,173
  • 3
  • 39
  • 66

1 Answers1

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

rodyhaddad
  • 186
  • 2