Can an angular directive pass arguments to functions in expressions specified in the directive's attributes?

Ed Hinchliffe picture Ed Hinchliffe · Nov 10, 2013 · Viewed 133.7k times · Source

I have a form directive that uses a specified callback attribute with an isolate scope:

scope: { callback: '&' }

It sits inside an ng-repeat so the expression I pass in includes the id of the object as an argument to the callback function:

<directive ng-repeat = "item in stuff" callback = "callback(item.id)"/>

When I've finished with the directive, it calls $scope.callback() from its controller function. For most cases this is fine, and it's all I want to do, but sometimes I'd like to add another argument from inside the directive itself.

Is there an angular expression that would allow this: $scope.callback(arg2), resulting in callback being called with arguments = [item.id, arg2]?

If not, what is the neatest way to do this?

I've found that this works:

<directive 
  ng-repeat = "item in stuff" 
  callback = "callback" 
  callback-arg="item.id"/>

With

scope { callback: '=', callbackArg: '=' }

and the directive calling

$scope.callback.apply(null, [$scope.callbackArg].concat([arg2, arg3]) );

But I don't think it's particularly neat and it involves puting extra stuff in the isolate scope.

Is there a better way?

Plunker playground here (have the console open).

Answer

Chandermani picture Chandermani · Nov 10, 2013

If you declare your callback as mentioned by @lex82 like

callback = "callback(item.id, arg2)"

You can call the callback method in the directive scope with object map and it would do the binding correctly. Like

scope.callback({arg2:"some value"});

without requiring for $parse. See my fiddle(console log) http://jsfiddle.net/k7czc/2/

Update: There is a small example of this in the documentation:

& or &attr - provides a way to execute an expression in the context of the parent scope. If no attr name is specified then the attribute name is assumed to be the same as the local name. Given and widget definition of scope: { localFn:'&myAttr' }, then isolate scope property localFn will point to a function wrapper for the count = count + value expression. Often it's desirable to pass data from the isolated scope via an expression and to the parent scope, this can be done by passing a map of local variable names and values into the expression wrapper fn. For example, if the expression is increment(amount) then we can specify the amount value by calling the localFn as localFn({amount: 22}).