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).
ng
API for exampleng-click="someFunction()"
is an expression that evaluates to executing a function. – Ed_$scope.callback
is set by thecallback="someFunction"
attribute and thescope: { callback: '=' }
property of the directive definition object.$scope.callback
is a function to be called at a later date. The actual attribute value is obviously a string - that is always the case with HTML. – Ed_