2

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?

3 Answers 3

8

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi @chrisrhoden, in my case, I have a div with contenteditable, so, I created the parameter to substitute "enter" event when this parameter is defined
1

chrisrhoden's answer is great. I would suggest to expand upon it a bit with the following. Doing so will help prevent ng-click double-firing issues relating to mobile devices and/or AngularJS conflicts with jQuery.

myApp.directive('responsiveClick', function(){
  return {
    scope: { myCb: '&' },
    link: function (scope, el,attr) {
  el.bind('touchstart click', function (e) {
    e.preventDefault();
    e.stopPropagation();
    scope.myCb({$event: e});
  });
  }
}
});

along with the markup as follows:

<a class="mdi-navigation-cancel" my-cb="removeBasketItem($event,item)" responsive-click></a>

Comments

0

have you tried passing it in the original function?

my-param="myFunc($event, param1, param2)"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.