Full answer
When you write (click)="save($event)", Angular listens to the browser event and passes the event object as $event. For native DOM events it is the same object you get from addEventListener: a MouseEvent, KeyboardEvent, InputEvent, SubmitEvent, and so on. For a component output, $event is whatever the child emitted, for example a number or an object.
Useful patterns:
- Key filters.
(keyup.enter)="add()"runs only when Enter is pressed, and(keydown.escape)="close()",(keydown.control.s)="save()"work in the same way. This is simpler than readingevent.keyyourself. - Reading the value.
event.targethas the typeEventTarget | null, so TypeScript does not know about.value. Put the conversion in a small method:onInput(event: Event) { const input = event.target as HTMLInputElement; this.name.set(input.value); }. A template reference variable is an alternative:<input #box (input)="name.set(box.value)" />. - Default actions. A link or form has a built-in action.
event.preventDefault()stops it. With(ngSubmit)on a form, Angular already prevents the page reload. - Bubbling. Events travel from the inner element to its parents.
event.stopPropagation()stops the event before it reaches parent handlers. Use it sparingly, because it hides events from other code. In a table row with a delete button, stopping propagation prevents the row click from also firing. - Several statements.
(click)="count = count + 1; log($event)"is allowed, but a method is easier to read and test.
Each handler call is followed by change detection, so the screen updates. Avoid heavy work in handlers that run often, such as mousemove and scroll. Debounce them, or run them outside Angular.
Typing: write handler parameters with the specific type (KeyboardEvent, MouseEvent). With strictTemplates, the compiler checks that $event matches what the handler expects.
import { Component, signal } from '@angular/core';
@Component({
selector: 'app-todo-list',
template: `
<input #box aria-label="New task" (keyup.enter)="add(box)" (input)="onInput($event)" />
<p>Typing: {{ typing() }}</p>
<ul>
@for (task of tasks(); track task) {
<li (click)="rowClicks.set(rowClicks() + 1)">
{{ task }}
<button type="button" (click)="remove(task, $event)">Delete</button>
</li>
}
</ul>
<a href="/help" (click)="openHelp($event)">Help</a>
`,
})
export class TodoListComponent {
readonly tasks = signal<string[]>([]);
readonly typing = signal('');
readonly rowClicks = signal(0);
defaultPrevented = false;
onInput(event: Event): void {
this.typing.set((event.target as HTMLInputElement).value);
}
add(box: HTMLInputElement): void {
const text = box.value.trim();
if (text) {
this.tasks.update((list) => [...list, text]);
box.value = '';
this.typing.set('');
}
}
remove(task: string, event: MouseEvent): void {
event.stopPropagation(); // the row click handler must not run too
this.tasks.update((list) => list.filter((t) => t !== task));
}
openHelp(event: MouseEvent): void {
event.preventDefault(); // stay on the page
this.defaultPrevented = event.defaultPrevented;
}
}Angular 20.3, strict template check passes. A Karma test sends an Enter keyup, a click on a button inside a row, and checks the list, that the row click does not fire, and that the link default action is prevented.
Key points
- `$event` is the native event for DOM events, or the emitted value for outputs.
- Use key filters such as `(keyup.enter)`. Cast `event.target` to the element type to read its value.
- `preventDefault` stops the browser action. `stopPropagation` stops bubbling.
- Avoid heavy work in frequent event handlers.
Common mistakes
- Reading `event.target.value` without a cast and fighting the compiler.
- Using `stopPropagation` everywhere and breaking other listeners.
- Calling `preventDefault` on `ngSubmit` when Angular already does it.
Follow-ups
- What is `$event` for a component output?
- How would you handle a keyboard shortcut like Ctrl+S?
- How do you stop a row click when a button inside the row is clicked?
Tips
- Show a typed handler and not `$any($event)`.
- Mention key filters. Many candidates forget them.
- Explain bubbling with the table row example.
Evaluation rubric
- Explains `$event` for both DOM events and outputs.
- Handles typing and key filters correctly.
- Understands default action and bubbling.