Event Binding
Event binding lets your component react to user actions — clicks, key presses, mouse movement, form submissions. The syntax uses parentheses around the event name: (event)="statement".
Handling a click
import { Component, signal } from '@angular/core';
@Component({
selector: 'app-counter',
template: `
<button (click)="decrement()">−</button>
<strong>{{ count() }}</strong>
<button (click)="increment()">+</button>
<button (click)="count.set(0)">Reset</button>
`,
})
export class Counter {
count = signal(5);
increment() {
this.count.update((c) => c + 1);
}
decrement() {
this.count.update((c) => Math.max(0, c - 1));
}
}The right-hand side is a template statement. Unlike binding expressions, statements may have side effects: call methods, set signals, or even run several statements separated by ;.
The $event object
Inside the statement, $event holds the native DOM event (or, for component outputs, the emitted value). Pass it to a handler to read details such as the input's text:
import { Component, signal } from '@angular/core';
@Component({
selector: 'app-live-search',
template: `
<input placeholder="Type a city…" (input)="onInput($event)" />
<p>You typed: <em>{{ query() }}</em> ({{ query().length }} chars)</p>
`,
})
export class LiveSearch {
query = signal('');
onInput(event: Event) {
const input = event.target as HTMLInputElement;
this.query.set(input.value);
}
}Tip: A template reference variable avoids the cast:
<input #box (input)="query.set(box.value)" />.
Key events with filters
Angular can filter keyboard events by key name, so you don't need if (event.key === 'Enter') in your handler:
| Binding | Fires when |
|---|---|
(keyup.enter) |
Enter is released |
(keydown.escape) |
Escape is pressed |
(keydown.shift.tab) |
Shift + Tab is pressed |
(keydown.control.s) |
Ctrl + S is pressed |
import { Component, signal } from '@angular/core';
@Component({
selector: 'app-quick-add',
template: `
<input #box placeholder="New tag, press Enter"
(keyup.enter)="add(box.value); box.value = ''"
(keydown.escape)="box.value = ''" />
<div>
@for (tag of tags(); track tag) {
<span class="tag">{{ tag }}</span>
}
</div>
`,
styles: `.tag { background: #ffe3e8; padding: 2px 8px; margin: 4px; border-radius: 8px; display: inline-block; }`,
})
export class QuickAdd {
tags = signal(['angular', 'signals']);
add(tag: string) {
const clean = tag.trim();
if (clean) this.tags.update((t) => [...t, clean]);
}
}The preview shows the state after typing rxjs and pressing Enter.
Under the hood: listeners
An event binding registers a listener on the element and calls your statement every time the event fires. Angular removes the listener automatically when the component is destroyed. The mechanism is the classic observer pattern, sketched here in plain TypeScript:
type Handler = (payload: string) => void;
class Emitter {
private handlers = new Map<string, Handler[]>();
on(eventName: string, handler: Handler): void {
const list = this.handlers.get(eventName) ?? [];
list.push(handler);
this.handlers.set(eventName, list);
}
emit(eventName: string, payload: string): void {
for (const h of this.handlers.get(eventName) ?? []) h(payload);
}
}
const button = new Emitter();
let clicks = 0;
button.on('click', () => clicks++);
button.on('click', (p) => console.log(`clicked with ${p}`));
button.emit('click', 'left button');
button.emit('click', 'keyboard');
button.emit('hover', 'ignored');
console.log('Total clicks:', clicks);clicked with left button
clicked with keyboard
Total clicks: 2
Common events
| Event | Typical element | Use |
|---|---|---|
(click) |
button, links, any element | Run an action |
(input) |
input, textarea | React to each keystroke |
(change) |
select, checkbox | React after a value is committed |
(submit) / (ngSubmit) |
form | Handle a form submission |
(focus) / (blur) |
inputs | Show hints, validate on leave |
(mouseenter) / (mouseleave) |
any element | Hover effects |
Test yourself
What does $event contain in (input)="onInput($event)"?
- The input's current value as a string
- The native DOM Event object
- The component instance
- Nothing; it is undefined
Which binding runs only when Enter is released?
(keyup)="enter()"(keyup.enter)="save()"[keyup.enter]="save()"(enter)="save()"
How is a template statement different from a binding expression?
- Statements can have side effects such as calling methods or setting signals
- Statements cannot call methods
- They are identical
- Statements must return a boolean