Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober 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 Scan×
Skip to content
World desk2 min

How to Add Event Listeners in Angular

Use template event bindings for most Angular UI interactions, pass $event when needed, and use the host property for component or directive host events.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For most Angular UI events, add a listener directly in the template: <button (click)="save()">Save</button>. Pass $event when the handler needs details about the event. For an event on a component or directive host, use the host property in its metadata; Angular says to prefer it over @HostListener.

Bind an event in an Angular template

Put the event name in parentheses and assign it a method call or other listener statement:

As an Amazon Associate I earn from qualifying purchases.

<button (click)="save()">Save</button>

Angular invokes the statement when the element emits that event. Common examples include click, keydown, and mouseover. The parentheses identify an event binding; they are distinct from property binding syntax such as [value]. Angular’s event-listener guide documents the syntax and examples.

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

Read the event object with $event

Pass $event to the handler when it needs information about the event, such as the key pressed or the target element:

<input (keyup)="updateField($event)" />

Type the handler argument to match the event you handle. For keyboard input, that can be KeyboardEvent; a handler can then inspect properties such as key.

Listen for a particular key

For common keyboard cases, add key modifiers after the event name, separated by periods:

<input (keyup.enter)="submit()" />
<input (keyup.shift.enter)="submit()" />

Angular supports modifier names including alt, control, meta, and shift. By default, key filters match keyboard key values—the meaning of a key, which can vary with layout or modifier state. Add the code suffix when you need to match a physical or virtual key code instead. See Angular’s event-listener guide and the HostListener API reference for modifier details.

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

Handle events on a component or directive host

When the event belongs to the component or directive’s host element, declare it in the decorator’s host property:

@Component({
  host: {
    '(click)': 'handleClick($event)',
    '(window:keydown.enter)': 'handleEnter($event)',
  },
})
export class Example {}

Global target prefixes documented by Angular are window:, document:, and body:. Angular’s current API guidance says to prefer the host property over @HostListener; the decorator remains documented for backwards compatibility. Angular’s HostListener API reference states: “Always prefer using the host property over @HostListener. This decorator exist exclusively for backwards compatibility.”

Prevent the browser’s default action

If the handler is replacing what the browser would normally do, call preventDefault() explicitly:

handleSubmit(event: Event) {
  event.preventDefault();
  // Handle the action in your application.
}

Angular also calls preventDefault() if a listener statement evaluates to false, but an explicit call makes the intent clearer. Angular’s event-listener guide documents both behaviors.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

When to use Renderer2.listen or a native DOM listener

Use template bindings or host metadata for ordinary component interactions: they express the event declaratively at the element or host where it belongs. Use an imperative listener only when the behavior genuinely needs direct DOM access. Renderer2.listen(target, eventName, callback) returns an unlisten function; retain and call that function when the registered listener should be disposed of.

Angular’s DOM APIs guide describes Renderer2’s special uses as narrow, including animation-related operations. It also states that Renderer2 DOM APIs do not support DOM manipulation in server-side rendering or build-time pre-rendering contexts. The documentation does not establish a complete, cross-version lifecycle recipe for every imperative native-listener scenario, so choose and manage such listeners according to the specific API and rendering context in your Angular version.

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.