I am working on a form directive that utilizes a specific callback
attribute with an isolate scope:
scope: { callback: '&' }
This directive is placed within an ng-repeat
, where the expression passed in includes the id
of the object as an argument to the callback function:
<directive ng-repeat = "item in stuff" callback = "callback(item.id)"/>
After the directive completes its task, it invokes $scope.callback()
from its controller function. While this suffices for most scenarios, there are times when I need to add another argument from inside the directive
itself.
Is there an Angular expression that would facilitate calling
$scope.callback(arg2)</code, so that <code>callback
is triggered with arguments = [item.id, arg2]
?
If not, what is a more elegant way to achieve this?
I have discovered that the following approach works:
<directive
ng-repeat = "item in stuff"
callback = "callback"
callback-arg="item.id"/>
Using
scope { callback: '=', callbackArg: '=' }
and having the directive execute:
$scope.callback.apply(null, [$scope.callbackArg].concat([arg2, arg3]) );
However, I believe this method can be improved upon and involves adding extra components to the isolate scope.
Is there a more efficient solution?
Access the Plunker playground here (make sure to have the console open).