AngularJS directive binding a function with multiple arguments

Alternative method that will survive minification

Leave your html as it was:

<my-directive on-drop="handleDrop"></my-directive>

Change the call to:

scope.onDrop()('123','125')

Notice the extra opening and closing parenthesis given to onDrop. This will instantiate the function instead of injecting the function’s code.

Why is it better

  1. Changing the parameters’ names in the handleDrop() definition (or even adding some more, if you handle it correctly) will not make you change each of the directives injections in the html. much DRYer.

  2. As @TrueWill suggested, I’m almost sure the other solutions will not survive minification, while this way code stays with maximum flexibility and is name agnostic.

Another personal reason is the object syntax, which makes me write much more code:

functionName({xName: x, yName: y}) // (and adding the function signature in every directive call)

As opposed to

functionName()(x,y) // (zero maintenance to your html)

I found this great solution here.

Leave a Comment

404 Not Found

Not Found

The requested URL was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.