0

Like the title asks, for what reasons would you use the ampersand binding in AngularJS instead of utilizing an event system--especially in a case where your application already uses events for some things? Either AngularJS's own $broadcast and $emit system or a third party library like Postal.

It seems like both paradigms accomplish the same goal of decoupling with the event system having the added bonus that more than one thing can "listen" for a thing to happen and respond to it.

  • I'm not an Angular expert, but in my experience setting up an event can be a bit more involved than doing a binding, so if you don't need the additional functionality, why make the effort? – Robert Harvey Oct 14 '17 at 14:47
  • In AngularJS, events are pretty simple, actually. You're point is still valid though. If you don't use events elsewhere already, why include them? What about a case where your code base already uses events (I've updated the question description to include this)? – Moismyname Oct 16 '17 at 15:58
  • You would still use the right tool for the right job. There isn't a carpenter in existence that restricts himself to a single hammer because it's the one he bought first. – Robert Harvey Oct 16 '17 at 16:02
1

AngularJS & binding is [expected to be]¹ very simple to understand, especially for a JavaScript developer.

In JavaScript, callbacks are used a lot. They are used so much, that it can be very disturbing to write JavaScript code for a Java or C# or PHP developer. When one would expect to have a return value, a given function doesn't return anything, but requires the caller to pass a callback which might be called somewhere in the future. NodeJS, in particular, is funny to use when coming from languages such as Java, C# or PHP, since some very simple problems, such as reading, consecutively, three files from disk, become very difficult to solve the callback's way.

This is exactly the approach of AngularJS. You pass a callback where you expect to be called, and that's it. You don't have to understand events. With a basic understanding of JavaScript's callbacks, you can do whatever you want. No need to learn some events library or framework.

AngularJS & binding is also more powerful than simple events. Like you highlighted in your question, it's not a callback binding, but an expression binding, which means that you can do more than just invoking a given event. I'm not sure if this flexibility has some important benefits, of if it's just a nuisance which makes AngularJS & binding harder than it needs to be; nevertheless, you can use it with expressions—something you won't be able to do if AngularJS was using events.

[But why would you use & binding] in a case where your application already uses events for some things?

To know the exact reason, ask the persons who maintained the code where you've seen both events and & binding. There could be several possible reasons:

  • Why use events, when & binding works as well? It is as simple as that.

  • The code may have started with & binding. Events were added where one needed them. Nobody would migrate the whole code base to use events, because it represents a cost for the business, while having absolutely no business value.

  • Some developers on the team are familiar with events; others prefer sticking with & binding.


¹ And then the dream breaks as soon as one realizes that those are not really callbacks, but expressions, and that it has some important consequences on the way the binding is used, even for things as simple as the function arguments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.