Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
-2
rated 0 times [  2] [ 4]  / answers: 1 / hits: 140068  / 11 Years ago, sun, november 10, 2013, 12:00:00

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).


More From » angularjs

 Answers
32

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}).



[#74378] Friday, November 8, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
andreguym

Total Points: 125
Total Questions: 112
Total Answers: 103

Location: Wallis and Futuna
Member since Tue, Mar 30, 2021
3 Years ago
;