Sub topic 6
Responding to User Events
This chapter assumes you know how browser DOM events work (click, input, keyup). It also builds on the interpolation and signal reading covered in the earlier chapters. Property binding sends data from the class into the template. Event binding goes the other way: it runs your code when the user does something.
The Event Binding Syntax
To bind an event, put the event name in parentheses on an element and assign it a template statement: (click)="save()". A template statement is a small piece of code that Angular runs each time the event fires. It usually calls a method on the component class.
Angular adds the DOM listener for you. When the component is destroyed, Angular removes the listener, so you don't need to clean it up yourself.
Reading Event Data with $event
Inside a template statement, $event holds the event payload. For native DOM events, the payload is the browser's Event object, such as a MouseEvent or a KeyboardEvent. Pass $event to a method and read the details in the class:
import { Component, signal } from '@angular/core';
@Component({
selector: 'app-greeter',
template: `
<input
placeholder="Your name"
(input)="onNameInput($event)"
(keyup.enter)="greet()" />
<button (click)="greet()">Greet</button>
<p>{{ message() }}</p>
`
})
export class GreeterComponent {
name = signal('');
message = signal('');
onNameInput(event: Event) {
const input = event.target as HTMLInputElement;
this.name.set(input.value);
}
greet() {
this.message.set(`Hello, ${this.name() || 'stranger'}!`);
}
}The cast to HTMLInputElement happens in the class because template statements do not support TypeScript's as syntax.
Key Event Filtering
(keyup.enter) is a key event filter. Angular runs the handler only when the user presses that key. You can combine modifier keys too, as in (keydown.shift.enter) or (keydown.control.s). Filters save you from writing if (event.key === 'Enter') checks by hand.
Common Errors
| Mistake | What happens | Fix |
|---|---|---|
| `(click)="greet"` | Angular reads the method reference and never calls it, so nothing happens | Write `(click)="greet()"` |
| `onNameInput()` declared with an `event` parameter but called without `$event` | `event` is `undefined` at runtime | Pass `$event` explicitly |
| `($event.target as HTMLInputElement).value` in the template | Template parse error | Do the cast in the class method |
| Using `(onclick)` | Angular listens for an event named `onclick`, which never fires | Use the bare event name: `(click)` |
Exercise
Add a "Clear" button to GreeterComponent that resets both signals. Then make pressing Escape in the input do the same thing. What template code and class method do you need?
Show answer
Add a method clear() { this.name.set(''); this.message.set(''); }. Add <button (click)="clear()">Clear</button> and the key filter (keyup.escape)="clear()" on the input. The input's visible text does not reset by itself, because nothing binds the signal back to the input. Add [value]="name()" to the input so the box empties when name resets.
Exercise
Why does (click)="greet" compile but do nothing when clicked?
Show answer
The template statement is evaluated as an expression. greet evaluates to the function reference and then throws it away. Nothing calls the function. You need the parentheses, greet(), to call it.