Event binding and template reference variables

Intermediate · 18 min

How a template reacts to user input, and how to grab a handle on an element without leaving the template.

Before thisInterpolation and property, attribute, class, and style binding

procademy · 18 min

Event binding and template reference variables

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by procademyReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • `(event)="handler($event)"` is the mirror image of property binding — data flowing out of the DOM into your component, instead of in.
  • A template reference variable (`#name`) gives you a handle on an element or directive instance, scoped to that template, with no component-class code required to get it.
  • `[(ngModel)]` is literally `[ngModel]` plus `(ngModelChange)` written as one combined syntax — understanding that decomposition demystifies every other two-way binding you'll build yourself.

Data has to flow both ways

Everything in the previous lesson moved data from your component out into the DOM — interpolation, property binding, all of it answering “how does the screen reflect my data.” But an interactive app needs the opposite direction too: a user clicks a button, types in a box, submits a form, and your component needs to find out. Templates need a syntax for “the DOM just told me something,” the same way they have one for “I’m telling the DOM something.”

Event binding: the DOM talking back

<button (click)="save()">Save</button>
<input (input)="onSearchChange($event)" />

(event)="handler()" wires a native DOM event to a method call. (click) is the browser’s own click event; (input), (keydown), (submit) — anything the DOM fires, Angular lets you bind to by name, in parentheses because parentheses are the syntax’s signal for “data flows out of the element, into my code” — the opposite of the square brackets you just learned for “data flows in.”

$event is the payload: whatever the native event handler would have received. For a plain click, that’s a MouseEvent; for input, an InputEvent. Angular isn’t inventing a new event system here — it’s giving you a declarative way to wire up addEventListener without writing addEventListener yourself, and $event is literally the same object the imperative version would hand you.

Template reference variables: a handle, without leaving the template

Sometimes you need to refer to one element from another part of the same template, without round-tripping through the component class at all:

<input #searchBox (keydown.enter)="search(searchBox.value)" />
<button (click)="searchBox.focus()">Focus search</button>

#searchBox declares a template reference variable — a name, scoped to this template, pointing at the DOM element it’s attached to (or, on a component or directive, an instance of that component/directive instead of a raw element). From that point on, anywhere else in the same template, searchBox means “that input.” No property on the component class had to exist for this to work; the reference lives entirely in template-land.

This is deliberately narrow. If you need that same reference inside your component’s TypeScript — not just elsewhere in the template — a template reference variable alone won’t get you there; that’s what @ViewChild is for, and it’s covered later in this roadmap once you’ve seen more of what there is to query.

[(ngModel)], decomposed

Two-way binding looks like its own special syntax, but it’s built from pieces you already have:

<input [(ngModel)]="username" />
<!-- is exactly equivalent to: -->
<input [ngModel]="username" (ngModelChange)="username = $event" />

[ngModel] is an ordinary property binding, pushing username’s current value into the input. (ngModelChange) is an ordinary event binding, firing whenever the input changes and writing the new value back. [( )] — “banana in a box,” as the inevitable nickname goes — is syntax sugar for having both of those at once. Once you see the two halves, two-way binding stops being a special case and becomes “property binding plus event binding, on the same name, bundled.” You’ll build the same pattern yourself later with custom components, using an input() paired with a matching output().

This lesson’s video covers event binding and template reference variables with several worked examples — the linked “deeper” video picks up with a more focused pass on event binding specifically if you want a second angle on it.

What’s next

You can now read data out of a component, push data in, and react when the user does something. The next few lessons zoom in on the component’s own lifecycle — when exactly your code runs relative to when Angular creates, updates, and destroys a component instance.

Check yourself

  1. 01In `<input (input)="onInput($event)">`, what is `$event`, concretely?

    The native DOM `Event` object the `input` event fired with — the same object you'd get from `addEventListener('input', e => ...)`. Angular isn't wrapping or replacing it; `$event` is just a template-syntax name for whatever argument the event handler received.

  2. 02What does `#searchBox` give you in `<input #searchBox>`, and where can you use it?

    It gives you a reference to that input's DOM element (or, on a component/directive host, an instance of that component/directive), usable anywhere else in the same template — for example `<button (click)="searchBox.focus()">`. It is not accessible from the component class's TypeScript code; for that, you need `@ViewChild`, covered later in this roadmap.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark