Sub topic 9
Loading on a condition with when
This chapter assumes you can write a standalone component, use @if and signals in a template, and have already written a basic @defer block with a @placeholder.
What the when trigger does
The on triggers react to the browser: idle time, visibility, a click. The when trigger reacts to your application state. You give it a boolean expression. The first time the expression becomes true, Angular starts loading the deferred chunk and then renders the block.
@defer (when user.isPremium) {
<app-premium-panel />
}While the expression is false, the block shows its @placeholder, or nothing if you didn't write one. Once it is true, the usual sequence runs: the @loading block appears if the download is slow, then the content replaces it, or @error appears if the download fails.
It fires once
The when trigger is one-way. If the expression later becomes false, the block does not go back to the placeholder. The swap happens once and stays.
The diagram below shows the states a block moves through.
If you need content that appears and disappears with state, wrap the @defer in an @if. The @defer handles the lazy loading, and the @if handles showing and hiding:
@if (showDetails()) {
@defer (when showDetails()) {
<app-details />
}
}After the first load, the chunk is cached by the browser, so re-creating the block later does not download it again.
A complete example
This component loads a heavy report only after the user turns on a toggle. The expression reads a signal, so Angular re-evaluates it when the signal changes.
import { Component, signal } from '@angular/core';
import { ReportComponent } from './report.component';
@Component({
selector: 'app-dashboard',
imports: [ReportComponent],
template: `
<label>
<input
type="checkbox"
[checked]="reportEnabled()"
(change)="reportEnabled.set(!reportEnabled())"
/>
Show report
</label>
@defer (when reportEnabled()) {
<app-report />
} @placeholder {
<p>Enable the report to load it.</p>
} @loading (after 100ms; minimum 1s) {
<p>Loading report...</p>
} @error {
<p>The report could not be loaded.</p>
}
`,
})
export class DashboardComponent {
reportEnabled = signal(false);
}Turning the checkbox on starts the download. Turning it off again leaves the report on screen, because the block is already complete.
Combining when with other triggers
You can list several triggers, separated by semicolons. Loading starts when any one of them fires.
@defer (when user.isPremium; on timer(10s)) {
<app-premium-panel />
} @placeholder {
<p>Premium features</p>
}Here the panel loads as soon as the user is premium, or after ten seconds, whichever comes first.
Prefetching with when
prefetch when downloads the chunk early but does not render it. Use it to separate "fetch the code" from "show the content":
@defer (when checkoutOpen(); prefetch when cartHasItems()) {
<app-checkout />
} @placeholder {
<p>Your cart</p>
}When the cart gets its first item, the chunk downloads in the background. When the user opens checkout, the block renders without a network wait.
Common errors
- Expecting a revert. The condition going back to false has no effect on a completed block. Use
@ifaround it if you need that. - A condition that is already true. The block loads right after the first render. Nothing is deferred in practice, so check that this is what you want.
- Referencing deferred code in the condition. The expression runs in the parent template. It cannot use anything that lives only inside the block.
- Heavy work in the expression. Angular evaluates the expression during change detection. Use a signal or a cheap property, not a method that does real computation.
- Forgetting the placeholder. Without one, the area is empty until loading starts, which can cause layout shift when the content appears.
Exercise
A block is written as @defer (when isOpen()). The user opens a panel so isOpen() becomes true, the content loads, and the user then closes the panel so isOpen() becomes false. What does the user see, and how would you make the content disappear when the panel closes?
Show answer
The deferred content stays visible, because a when trigger fires once and never reverts to the placeholder. To hide it on close, wrap the @defer block in @if (isOpen()) { ... }. The chunk is already cached, so reopening does not download it again.