Sub topic 2
What Angular compares: template bindings
This chapter assumes you know Angular components, template syntax (interpolation, property bindings and event bindings), and component inputs. It explains what Angular looks at when it checks a component, which is the first step toward understanding why a view updates or does not.
A binding is a stored value plus an expression
Each binding in a template is compiled into an instruction. The instruction evaluates the expression, compares the result with the value from the previous check, and writes to the DOM only if the two differ.
Take this template:
import { Component } from '@angular/core';
@Component({
selector: 'app-profile',
template: `
<h2>{{ name }}</h2>
<img [src]="avatarUrl" [alt]="name" />
<button (click)="rename()">Rename</button>
`,
})
export class Profile {
name = 'Ada';
avatarUrl = '/avatars/ada.png';
rename() {
this.name = 'Grace';
}
}The view of Profile holds one stored value for each of the three bindings: {{ name }}, [src] and [alt]. On each check Angular evaluates the three expressions again. If name is now 'Grace', the stored value for {{ name }} and [alt] differs, so Angular updates the text node and the alt property. [src] is unchanged, so Angular does not touch it.
The event binding (click) works differently. It is not compared. Angular registers the listener once, and the listener runs when the event fires.
How values are compared
Angular compares the old and new values with Object.is. This is close to ===, with two differences: NaN equals NaN, and +0 does not equal -0.
| Value type | What "changed" means |
|---|---|
| string, number, boolean, `null`, `undefined` | The value is different. |
| object, array, function | The reference is different. |
For objects, the comparison is shallow. Angular does not look inside an object to see whether a property changed. If a template binds [user]="user" and you change user.name in place, the reference stays the same, so Angular sees no change in that binding.
This does not mean a mutation is invisible everywhere. If the template reads {{ user.name }}, that expression yields a string. Angular compares the string, and the text updates. The reference comparison matters when the binding passes the whole object to a child component's input. The next chapters build on that distinction.
Expressions run on every check
Angular evaluates every binding expression each time it checks the component. That is how it finds out whether the result changed. Two consequences follow.
- Expressions must be cheap. A method call in a template runs on every check.
- Expressions should return the same value when nothing has changed.
The second point has a common failure. This binding creates a new array on every evaluation:
import { Component } from '@angular/core';
@Component({
selector: 'app-list',
template: `<app-row [items]="getItems()" />`,
})
export class List {
getItems() {
return [1, 2, 3]; // a new array reference on every call
}
}The values look identical, but the references differ, so Angular sees a change every time. The child input is updated on every check. In development mode this can also lead to an ExpressionChangedAfterItHasBeenChecked error, which a later chapter covers. The fix is to keep the array in a field or compute it once.
What a check does and does not do
Checking a component means evaluating its bindings and comparing them. It does not re-render the component. DOM writes happen only for bindings whose values changed. A check that finds nothing new is cheap but not free, because every expression still runs.
The diagram shows the flow for one binding.
Summary of the model
- Each property, attribute, text and input binding has a stored value from the last check.
- Angular re-evaluates the expression and compares with
Object.is. - Objects are compared by reference, not by content.
- Only changed bindings cause DOM or input updates.
- Event bindings are listeners, not compared values.
Exercise
A parent template contains <app-card [user]="user" />, and a method runs this.user.name = 'Grace'. The card's template contains <p>{{ user().name }}</p> and the card uses the Default strategy. Which binding does Angular compare in the parent, and does that comparison see the mutation? Does the text in the card still update?
Show answer
In the parent, Angular compares the [user] binding, whose value is the object reference. The reference is unchanged, so that comparison sees no change and the input is not updated. The text still updates, because the card is checked under the Default strategy. There Angular evaluates user().name, gets the string 'Grace', finds it differs from the stored 'Ada', and updates the text node.