liveBook · Manning

Accessor method

As explained in Pro Angular 16

A getter is a class or object member that computes and returns a value when accessed as a property.

An accessor method, commonly written as a getter, computes and returns a value when code or a template reads it like a property. In an Angular component, a getter can expose derived data without requiring parentheses in the template. section 9.6.2 section 10.2

Deriving values for a template

A component can calculate product information from its model and expose the results through getters: get count(): number { return this.model.getProducts().length; } get total(): string { return this.model.getProducts() .reduce((total, p) => total + (p.price ?? 0), 0) .toFixed(2); } get message(): string { return `${this.messages[this.index]} $${this.total}`; } The template reads these values with {{ count }} and {{ message }}. Angular evaluates the bindings, reads the component values, and creates the displayed DOM content. section 10.2

Getters are convenient for derived display values, but their bodies may run whenever Angular checks the template. A costly getter therefore repeats its work even when unrelated changes occur, causing pauses and making binding performance harder to manage. section 10.3.2

Signals provide an alternative when derived values should update selectively. A getter can expose a read-only signal, while a computed signal replaces a frequently reevaluated getter and is read with function-call syntax such as {{ count() }}. section 10.4.4

How Pro Angular 16 covers it

How deep it goes
Explains getter syntax, template evaluation, performance costs, and a signal-based alternative, but not language-level accessor internals.
Examples
Uses product count, total price, message selection, template interpolation, and an intentionally expensive counting loop.
What it assumes
Assumes basic TypeScript classes and Angular templates while demonstrating component bindings from a simple product model.
What you can do after
Evaluate when to expose derived template data with getters and when to replace repeated work with computed signals.
How it connects
Connects getters with Angular components, templates, data binding, change detection, signals, and computed signals.

Also explained in

Compare how 13 books explain accessor method →

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.