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

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

For events on elements in an Angular template, use a template binding such as (click)="save()". Pass $event when the handler needs the event object. For an event on a component or directive’s host element, Angular’s current guidance is to use the decorator’s host property.

Bind an event in an Angular template

Put the event name in parentheses and assign a handler statement. Angular calls that statement when the element emits the event:

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

This pattern works for events such as click, keydown, and mouseover. The binding belongs on the template element whose event you want to handle. See Angular’s event listener guide.

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

Pass the event object to the handler

Use the special $event value when your handler needs details about the event. For a keyboard event, the handler can receive a KeyboardEvent and inspect its key property:

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

Choose the relevant DOM event type in the component method when you want TypeScript to describe the event’s properties.

Listen for a particular keyboard key

Angular supports key modifiers separated by periods. For example, (keyup.enter)="submit()" runs the handler for Enter, while (keyup.shift.enter)="submit()" requires Shift and Enter together.

Modifiers include alt, control, meta, and shift. By default, Angular matches the keyboard event’s key value. Use the code suffix when you need to match a physical or virtual key code independently of the character produced by the keyboard layout or modifier state. The distinction matters when the desired behavior is tied to a key position rather than the character reported by the keyboard.

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

Listen 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. It can also listen on documented global targets using the window:, document:, or body: prefix:

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

Angular still documents @HostListener, which declares a host event handler and can accept an event name plus optional arguments. However, the HostListener API documentation says to prefer the host property and describes the decorator as existing for backwards compatibility. For new host listeners, follow that guidance.

Prevent the browser’s default action

If your handler is replacing a browser default—such as the normal action associated with an event—call event.preventDefault() explicitly in the handler. Angular also calls it when a listener statement evaluates to false, but an explicit call makes the intent clear in the code.

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

When to use Renderer2 or a native listener

Use template bindings for events on template elements and host metadata for host-level events when those declarative forms express the behavior. They make the event source and handler visible in the component or directive declaration.

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

For a case that genuinely requires imperative DOM access, Renderer2.listen(target, eventName, callback) returns an unlisten function that can remove the registered listener. Do not choose it merely as a substitute for an ordinary template binding: Angular describes Renderer2’s special uses as narrow, and its DOM APIs do not support DOM manipulation in server-side rendering or build-time pre-rendering contexts. Consult Angular’s DOM APIs guidance before relying on DOM operations in those rendering contexts. The documentation cited here does not establish a complete lifecycle recipe for every imperative native-listener scenario.

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.