liveBook · Manning

Event

As explained in Pro Angular 16

A browser event is an object representing an occurrence that can provide information to event handlers about the occurrence and its source.

A browser event reports an occurrence to a handler. In an Angular template, an event binding such as `(input)` runs an expression when the browser triggers that event. The template variable `$event` provides the browser’s Event object, including the element that triggered it. section 13.2.1

Table 13.3. The properties common to all DOM event objects
Name
Description
type
This property returns a string that identifies the type of event that has been triggered.
target
This property returns the object that triggered the event, which will generally be the object that represents the HTML element in the DOM.
timeStamp
This property returns a number that contains the time that the event was triggered, expressed as milliseconds since January 1, 1970.

The shared Event properties identify the event type, its target, and when it occurred. More specific event objects provide details beyond these common properties. section 13.2.1

Read an input event

An input can update a signal with `(input)="selectedProduct.set($any($event).target.value)"`. The browser supplies an InputEvent whose target is the input element, but Angular checks `$event` as the more general Event type. `$any` permits access to `value` by disabling type checking for that expression. section 13.2.1

To keep type checking, pass the event to a component method with `(input)="handleInputEvent($event)"`. The method accepts an `Event`, checks `ev.target instanceof HTMLInputElement`, and only then reads `ev.target.value` to update the signal. section 13.2.2

Choose when a handler should run. `(input)` responds as content changes; `(keyup)` responds when a key is released. A binding such as `(keyup.enter)` waits for Enter, avoiding an application-state update on every keystroke. section 13.2.3

Emit and receive a custom event

A directive can respond to a host element’s DOM click by calling `this.click.emit(this.product.category)` on an `EventEmitter<string>` exposed through `@Output("pa-category")`. A template binding, `(pa-category)="newProduct.category = $event"`, receives the emitted category. Here `$event` is a string, not the browser’s click Event object. section 14.4 section 14.4.1

How Pro Angular 16 covers it

How deep it goes
Defines shared Event properties and works through input handling and directive output without detailing browser event-dispatch internals.
Examples
Demonstrates product-name input, Enter-key filtering, and a table-row click that sends a product category to a form.
What it assumes
Assumes Angular templates and component methods while introducing the browser Event object and its shared properties.
What you can do after
Apply it via Angular event bindings and EventEmitter to read input with a type guard and pass values from directives.
How it connects
Connects events to Angular event binding, signals, DOM elements, attribute directives, output properties, and EventEmitter.

Also explained in

Compare how 18 books explain event →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro Angular 16. The section links point to the parts of the book it draws on.