Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling function inside angular directive and inject event in parameters

I need your help.

I have a directive with a function parameter ( scope: { myParam: '@'}). And I'm passing the parameters in HTML, like my-param="myFunc(param1, param2)"

This works perfectly. But, I need to inject the event object in to the parameters. Does some one know how can I do that?

I tried $provider.annotate and $provider.instantiate, but they did not work because it's taking the reference function in directive. ($a in my case), so it can't get the function arguments.

any idea?

like image 524
ops.rio Avatar asked Jan 20 '26 00:01

ops.rio


1 Answers

When you're calling a function created by the & isolate scope syntax, you can pass parameters to it as a named map. If your directive is defined like this:

scope: { myParam: '&' },
link: function (scope, el) {
  el.on('click', function (e) {
    scope.myParam({$event: e});
  });
}

and used like this:

<my-directive my-param="console.log($event)"></my-directive>

... you should see the desired behavior.

like image 133
chrisrhoden Avatar answered Jan 22 '26 19:01

chrisrhoden



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!