October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

Event Handling in Angular: Template Events, Outputs, and Host Listeners

Use Angular’s parenthesized template syntax for browser events, outputs for component-to-parent communication, and host listeners for host-level events.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
<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.

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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:

<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.

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::

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
@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.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.