To respond to a browser event in an Angular template, put the event name in parentheses: <button (click)="save()">. Pass $event when the handler needs details from the browser event. For a child component’s own event, define an output and handle it in the parent; for events on a component or directive host, use host listeners.
Listen for browser events in a template
Angular’s event-listener syntax puts the event name in parentheses and assigns it a template statement. For example, <button (click)="save()">Save</button> calls the component’s save() method when the button is clicked. This is the basic way to respond to user actions such as button presses and form submissions.
As an Amazon Associate I earn from qualifying purchases.
Every template event listener can access $event, which refers to the browser event object. Pass it to the handler only when the handler needs the event’s details:
Do these 3 things before closing this tab:
1Fix the driver behind crashes, sound loss and screen glitches2Repair Windows errors before they cause bigger problems3Scan for outdated or missing drivers - takes under a minute<button (click)="logClick($event)">Details</button>
For keyboard events, Angular supports key modifiers such as .enter and combinations such as .shift.enter. For example, (keydown.shift.enter)="submit()" listens for Shift+Enter. A .code suffix matches KeyboardEvent.code, which identifies a physical or virtual key; the key value can vary with the keyboard layout or modifiers. Choose .code when matching the key’s position is the goal, rather than the character it produces. See Angular’s event-listener guide.
#1 Best Overall
Replace a browser’s default action
If a handler should replace what the browser normally does—for example, following a link or submitting a form—call preventDefault() on the event:
handleSubmit(event: Event) {
event.preventDefault();
// Handle the submission here.
}
Angular also calls preventDefault() if a listener expression evaluates to false, but an explicit call makes the intent clear.
Rank #2
Send a component event to its parent
Use an output when a component needs to report a component-specific action or value to its parent. In new code, Angular recommends declaring outputs with output() and emitting a value with .emit():
Free tools Windows power users keep installed
One-click scans. No signup required.
import { Component, output } from '@angular/core';
@Component({
selector: 'app-save-button',
template: '<button (click)="saved.emit(true)">Save</button>',
})
export class SaveButton {
saved = output<boolean>();
}
The parent listens using the output’s name in parentheses. Its $event receives the emitted value:
Rank #3
<app-save-button (saved)="handleSaved($event)" />
Angular outputs are case-sensitive, and custom outputs do not bubble up the DOM. A parent must listen on the component that declares the output; it should not expect the event to travel through ancestor elements like a bubbling browser event. Avoid output names that collide with native DOM events, use camelCase, and do not prefix names with on. The custom outputs guide explains the pattern.
Keep existing EventEmitter outputs working
@Output() with EventEmitter remains supported. Existing components can continue to emit with .emit(value); Angular recommends output() for new projects. The output API reference marks output() stable since Angular v19.0.
Rank #4
Listen on a component or directive host
When the event belongs to the component or directive’s host element rather than an element in its template, declare a listener in the host property. A host listener can also target supported global objects with prefixes such as window:, document:, and body::
@Component({
selector: 'app-example',
host: {
'(window:click)': 'handleWindowClick($event)',
},
template: '<p>Example</p>',
})
export class Example {
handleWindowClick(event: MouseEvent) {
// Respond to a window click.
}
}
Angular’s HostListener API reference says to prefer the host property over @HostListener; the decorator remains available for backwards compatibility.
Choose the event mechanism that matches the job
| Need | Angular mechanism | What it does |
|---|---|---|
| Respond to a browser event on a template element | (eventName)="handler($event)" |
Calls a template statement; $event provides the event object. |
| Notify a parent about a component-specific action or value | output() and (outputName)="handler($event)" |
Emits the component’s value; custom outputs do not bubble. |
| Maintain an existing decorator-based output | @Output() with EventEmitter<T> |
Still supported; emit using .emit(). |
| Listen on a component or directive host, including a supported global target | host: {'(window:click)': 'handler($event)'} |
Declares a host-level listener; prefer this to @HostListener. |
For additional beginner examples, Angular’s event-handling tutorial covers adding listeners in response to user interaction.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




