Sub topic 2
What Angular compares: template bindings
This chapter assumes you can write standalone components with templates, interpolation, property bindings, and event bindings. It also assumes you know how @Input or input() passes data from parent to child. The chapter explains what Angular stores for each binding and what it compares during a check. The rest of change detection builds on this.
A check compares values, not the DOM
Angular does not diff your component's fields. It also does not diff the DOM. When it checks a component, it re-evaluates each binding expression in that component's template. It then compares each result with the value it stored the last time that binding was checked.
If the new value is the same, nothing happens. If it differs, Angular stores the new value and applies it. For a text binding it updates the text node. For a property binding it sets the DOM property. For a child component input it sets the input.
Because Angular compares against its own stored value, it never reads the DOM to decide what to do. If other code changes a text node that Angular manages, Angular will not repair it. The stored value still matches the expression, so Angular sees no difference.
Which bindings are compared
Angular stores one slot per dynamic binding in the component's view data. Different binding types use these slots in the same way.
| Binding | Example | Compared on each check? | What happens on a difference |
|---|---|---|---|
| Interpolation | `{{ user.name }}` | Yes, every expression in it | Text node is updated |
| Property | `[disabled]="busy"` | Yes | DOM property is set |
| Input of a child component | `[user]="user"` | Yes | Input is set on the child |
| Attribute | `[attr.aria-label]="label"` | Yes | Attribute is set or removed |
| Class and style | `[class.active]="on"`, `[style.width.px]="w"` | Yes | Class or style is updated |
| Static attribute | `title="Profile"` | No, set once | Never changes |
| Event | `(click)="save()"` | No | Listener is registered once |
Event bindings do not take part in comparison. Angular attaches the listener when the view is created. Running the handler is a separate matter. It is one of the things that can cause a check, as later chapters cover.
An interpolation with several expressions, such as {{ first }} {{ last }}, compares each expression separately. Angular rebuilds the string and updates the text node if any of them changed.
The comparison is Object.is
Angular compares old and new values with Object.is. This is close to ===, with two differences that rarely matter in templates:
NaNequalsNaN. A binding that evaluates toNaNdoes not look "changed" on every check.+0and-0are different values.
The consequence for objects and arrays is that Angular compares references, not contents. Two objects with identical fields are different values if they are different objects. The same object with a changed field is the same value.
Whether this matters depends on what the binding reads:
{{ user.name }}evaluates to a string. If you mutateuser.name, the string changes, and the text is updated.[user]="user"evaluates to the object. If you mutateuser.name, the reference is the same, so Angular sees no change to that input.
That second case is the root of the OnPush behavior covered in a later chapter. Under the Default strategy you often don't notice it. The child component is checked anyway, and its own {{ user().name }} binding picks up the new string.
A worked example: counting evaluations
This component has an interpolation, a method call, and an event binding. The method logs each time Angular evaluates it.
import { Component } from '@angular/core';
@Component({
selector: 'app-profile',
template: `
<h2>{{ user.name }}</h2>
<p>{{ greeting() }}</p>
<button (click)="rename()">Rename</button>
`,
})
export class Profile {
user = { name: 'Ada' };
greeting(): string {
console.log('greeting evaluated');
return `Hello, ${this.user.name}`;
}
rename(): void {
this.user.name = 'Grace';
}
}When you run this in development mode, greeting evaluated appears twice on startup. It appears again each time any event, such as a click, triggers a check. The second log in each cycle comes from a verification pass that Angular runs only in development mode. It evaluates every binding again to confirm that nothing changed during the check. The last section of this chapter returns to that pass.
The console output shows an important fact: Angular must call greeting() to get a value to compare. It cannot know in advance whether the result changed. A method call in a template therefore runs on every check of that component, even when nothing it depends on has changed. For cheap methods this is fine. For expensive work, move the computation into a pure pipe, a computed signal, or a field you update when the data changes.
Pure pipes skip work by comparing arguments
A pipe is pure by default. Angular calls a pure pipe's transform only when its input value, or one of its arguments, differs from the previous call according to Object.is. Otherwise Angular reuses the last result.
import { Component, Pipe, PipeTransform } from '@angular/core';
@Pipe({ name: 'initials' })
export class InitialsPipe implements PipeTransform {
transform(name: string): string {
console.log('initials computed');
return name
.split(' ')
.map((part) => part[0])
.join('');
}
}
@Component({
selector: 'app-badge',
imports: [InitialsPipe],
template: `
<span>{{ fullName | initials }}</span>
<button (click)="tick()">Tick {{ ticks }}</button>
`,
})
export class Badge {
fullName = 'Ada Lovelace';
ticks = 0;
tick(): void {
this.ticks++;
}
}Each click re-evaluates the binding fullName | initials, because the click causes a check. The fullName string is unchanged, so Angular does not call transform again. initials computed is logged once.
The same rule affects objects. A pure pipe that receives an array will not rerun if you push to that array, because the reference is the same. This is a frequent source of "my pipe doesn't update" bugs. Pass a new array, or use an impure pipe (pure: false). An impure pipe runs on every check, like a method call.
Fresh objects and arrays defeat the comparison
The comparison depends on stable references. A binding that creates a new object each time it is evaluated always looks changed.
import { Component } from '@angular/core';
@Component({
selector: 'app-list-host',
template: `<ul>
@for (item of visibleItems(); track item) {
<li>{{ item }}</li>
}
</ul>`,
})
export class ListHost {
items = ['a', 'b', 'c', 'd'];
visibleItems(): string[] {
return this.items.filter((item) => item !== 'c'); // new array on every call
}
}filter returns a new array each time visibleItems() runs. If you bound that result to a child component's input, the reference would differ on every check, and the child input would be set every time. Under OnPush, that makes the child dirty on every parent check, which cancels the benefit of OnPush.
Two details help here:
- Angular memoizes object and array literals written directly in a template. With
[config]="{ size: large }", it builds a new object only whenlargechanges. So literals are usually safe. - The development-mode verification pass treats two different objects as equal. It also treats two arrays with equal contents as equal. So fresh references normally do not produce an
ExpressionChangedAfterItHasBeenCheckederror. The cost is silent: extra input updates and extra work in children.
The fix is to compute the derived value once, when its source changes, and bind to the stored result. A computed signal does this:
import { Component, computed, signal } from '@angular/core';
@Component({
selector: 'app-list-host',
template: `<ul>
@for (item of visibleItems(); track item) {
<li>{{ item }}</li>
}
</ul>`,
})
export class ListHost {
items = signal(['a', 'b', 'c', 'd']);
visibleItems = computed(() => this.items().filter((item) => item !== 'c'));
}visibleItems() returns the same array until items changes.
First check and the verification pass
Two more details complete the model.
On the first check of a view, Angular has no previous value. Each binding's slot starts with a special "no value yet" marker. Every binding therefore counts as changed, and all initial values are applied.
In development mode, Angular runs a second pass after each check. It re-evaluates the bindings and compares them against the values just stored. If a binding differs, Angular throws ExpressionChangedAfterItHasBeenChecked. That error means a value changed during the check, after Angular had already compared and stored it. The ExpressionChangedAfterItHasBeenChecked chapter covers the causes and fixes. Production builds skip this pass, which is also why method calls log once per check there instead of twice.
Exercise
A parent template contains [user]="user" for a child component. A click handler runs this.user.name = 'Grace'. Explain why the child's input binding sees no change, and why a child template containing {{ user().name }} can still show the new name under the Default strategy.
Show answer
The input binding evaluates to the user object. Mutation keeps the same reference, so Object.is reports no difference and Angular does not set the input again. Under the Default strategy, Angular still checks the child. The child's {{ user().name }} binding evaluates to a string. 'Grace' differs from the stored 'Ada', so the text node is updated.
Exercise
A component template contains {{ expensiveLabel() }}. The method takes 20 ms and its result only depends on a field status that changes rarely. Name two ways to avoid running the computation on every check, and say how each relies on Angular's comparison.
Show answer
Use a pure pipe such as {{ status | label }}, because Angular calls transform only when status differs from its previous value by Object.is. Or define label = computed(() => ...) from a status signal and bind {{ label() }}. The computed signal caches its result until status changes, and reading it is cheap, so the binding comparison is quick.