Angular: Bind callback function using & and pass-in arguments

Jeanluca Scaljeri picture Jeanluca Scaljeri · Oct 21, 2015 · Viewed 21.5k times · Source

I have a (simplified) directive

angular.module('myApp')
   .directive('myButton', function () {
        return {
            restrict: 'E',
            scope: {
                callbackFn: '&'
            },
            template: '<button ng-click=ca;;backFn($evenb)'
        }
   });

Now, in some parent controller I have defined a callback function:

this.myCallback = function ($event) {
    this.doIt($event);
}

and the HTML:

<my-button callback-fn="page.myCallback()"></my-button>

(I'm using things like bindToController and controllerAs)

The issue is that the $event is never passed to myCallback, which most likely has to do with how I bind this function (&). But on the other hand, inside myCallback I would like to use this.

Is there some way to fix this ? without doing things like

var self = this;
this.myCallback = function ($event) {
     self.doIt($event);
}

Answer

ryanyuyu picture ryanyuyu · Oct 21, 2015

You haven't completely set up your bindings correctly. You can pass back arguments from the directive to the parent controller via a key-value map. According to the angular docs (emphasis mine):

& 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 <widget my-attr="count = count + value"> 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 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}).

So that means in your consuming HTML you need to add parameters:

<my-button callback-fn="page.myCallback(parentEvent)"></my-button>

And then in the directive:

......
restrict: 'E',
scope: {
            callbackFn: '&'
       },
template: '<button ng-click="ctrl.callbackFn({parentEvent: $event})">Callback</button>'

,