title: Put the knife down and take a green herb, dude. |
descrip: One feller's views on the state of everyday computer science & its application (and now, OTHER STUFF) who isn't rich enough to shell out for www.myfreakinfirst-andlast-name.com Using 89% of the same design the blog had in 2001. |
FOR ENTERTAINMENT PURPOSES ONLY!!!
Back-up your data and, when you bike, always wear white. As an Amazon Associate, I earn from qualifying purchases. Affiliate links in green. |
|
x
MarkUpDown is the best Markdown editor for professionals on Windows 10. It includes two-pane live preview, in-app uploads to imgur for image hosting, and MultiMarkdown table support. Features you won't find anywhere else include...
You've wasted more than $15 of your time looking for a great Markdown editor. Stop looking. MarkUpDown is the app you're looking for. Learn more or head over to the 'Store now! |
|
Wednesday, April 14, 2021 | |
Any time I forget something more than once, I should probably blog about it. Saves me time looking for a comment on SO I put on an answer I particularly appreciated. Because I've forgotten this particular something more than once, let's return to our time machine, travel back to AngularJS land again, and leave some notes. What's an "ampersand binding" in AngularJS?Essentially, an ampersand binding allows you to mark that you're passing an event handler function to a custom component. Here's one mention in the AngularJS docs:
(There may be a better reference in the docs, but man it's hard to google an ampersand. They could have also ensured something that made it easier to find, like if it had a clear name similar to "ampersand component binding", but the AngularJS folk weren't nice enough to do that either afacit.) Here's some example code [of mine :shudder:] that uses it:
In this case, we've got a custom checkbox component that supports tristate. Specifically, that means it's a wrapper that enables easy access to checked, unchecked, and indeterminate states (three states, not the usual two. Am I over explaining this? Indeterminate status checkboxes are kinda complicated in vanilla HTML, and this is trying to abstract some of that in a package AngularJS can consume easily. As a freakin' huge proponent of props down, events up, the tristate checkbox is a very black box.
Where the ampersand binding bites youThe problem is that the ampersand binding doesn't give a function for your component's controller that you can call in a conventional sense. If
... you'd never guess how you pass Here's an example (again, this is a real-world example. It's a little more use case specific than I'd like for a perfect demo):
But How do I pass it? Like this:
Inside of an object literal? Wait, WTFOMGBBQNSFW!!!!! At first, I would've expected the event handler registration in the template to look like this:
... and the call from within the component to look like this...
But it's not. I would have also accepted something in the component that lets you know you're calling a weirdly wrapped event handler prop, something like...
That's funky, but it has the benefit of letting you know something funky is going on. I mean, if Um, no. No. Not at all. Instead, we have to pass that
and
Instead of letting us call the handler directly, AngularJS provides some weird monkeypatching that makes The advantages of the AngularJS ampersand binding conventionIt's not all bad. What's neat about this is that it does allow us to combine jive from the template scope, like our That is, by not limiting the call to the event handler from within the component to the literal event handler signature, we've got a lot more flexibility. We can combine two scopes, custom component controller and template contexts. At the same time, though passing an object literal that maps to parameters named only in a prop definition in a template file might be more flexible, it's an extremely unintuitive, indiscoverable convention. That we were establishing a new convention and why really didn't click for me until I understood how the new convention was useful -- precisely so that we could mix parent template and component controller scopes like this. Put another way...If that didn't click, here's a good explanation from superluminary on SO of the [unintuitive] way an ampersand binding in AngularJS works, and is what finally made how things worked click in my head. Once I had this convention straight and was able to combine it with the advantages of in-template parameter mixing, I was finally an ampersand binding expert. (Until I forgot.) Anyhow, from that answer:
It's almost like AngularJS said, "I see your event handler, and I'm going to raise you this monkeypatch..."
(Actually, that's functionally speaking exactly what AngularJS does, though there's another round of mapping after you compiled the template to code before you can have What people usually end up doing... and why you shouldn'tThe weird part is that you can skip this new convention entirely if you pass the event handler by reference. It's cheating, I'm pretty sure the AngularJS wishes you wouldn't, and you lose a lot of flexibility, but I can see why people do it. The ampersand binding convention simply isn't easily intuitive. Here's one such example of that "my mind is blown; gimme simple" mentality from krawaller.se:
Then, in your controller, you'd simply call But recall that you now can't do anything clever with your template's scope! As long as the template is something trivial like the Anyhow, I've known the ampersand binding convention long enough to use it and forget it once already, so there's a refresher. Hope that's helpful if you too have been sucked INTO AN ANACHRONISTIC ANGULARJS TIME BOB OMB!!!!1!!! Labels: angularjs, noteToSelf posted by ruffin at 4/14/2021 01:42:00 PM |
|
| |
All posts can be accessed here: Just the last year o' posts: |
||||||||||||||||||||||
|