Sub topic 4
Showing state with @placeholder, @loading and @error
This chapter assumes you know standalone components, the imports array, and basic template control flow. You should also have written a first @defer block. A deferred block has a lifecycle, and Angular lets you declare what the user sees at each stage.
The four states of a deferred block
A @defer block moves through these states:
- Placeholder: nothing has been requested yet. The trigger has not fired.
- Loading: the trigger fired and the chunk is downloading.
- Complete: the chunk arrived and the main content renders.
- Error: the download failed.
Each state except complete has an optional sub-block that holds its content. The main block is the complete state.
Transitions only go forward. Once a block leaves the placeholder state it never returns to it.
@placeholder
The placeholder renders before loading starts. Without one, the area is empty until the main content appears.
@defer (on viewport) {
<app-comments [postId]="postId" />
} @placeholder {
<div class="comments-placeholder"></div>
}The placeholder matters for two reasons. It reserves space, which limits layout shift. It is also the element that the on viewport, on interaction and on hover triggers watch by default. For on viewport to work without a parameter, the placeholder must contain exactly one root element. A placeholder that holds only text gives the trigger nothing to observe.
@loading
The loading block renders while the chunk downloads.
@defer (on interaction(showChart)) {
<app-chart />
} @placeholder {
<p>Press the button to see the chart.</p>
} @loading {
<app-spinner />
}On a fast connection the chunk may arrive in a few milliseconds. The loading block then appears and disappears almost at once. The next section addresses that.
@error
If the chunk request fails, for example because of a network drop or a stale deployment, Angular renders the error block.
@defer (on viewport) {
<app-comments [postId]="postId" />
} @error {
<p>Comments could not be loaded.</p>
}Without an @error block, a failed load leaves the placeholder or loading content in place and gives the user no signal. Add @error to any block whose content matters.
Timing parameters
The minimum and after parameters stop quick state changes from flickering. Both take a duration such as 500ms or 1s.
| Block | Parameter | Meaning |
|---|---|---|
| `@placeholder` | `minimum` | Show the placeholder for at least this long once it renders |
| `@loading` | `after` | Wait this long after loading starts before showing the loading block |
| `@loading` | `minimum` | Once shown, keep the loading block at least this long |
Here they appear together:
@defer (on viewport) {
<app-comments [postId]="postId" />
} @placeholder (minimum 500ms) {
<p>Comments load when this area is visible.</p>
} @loading (after 100ms; minimum 1s) {
<app-spinner />
} @error {
<p>Comments could not be loaded.</p>
}Follow what happens on a fast load. The placeholder shows for 500 ms even if the trigger fires immediately. If the chunk arrives within 100 ms of loading starting, the spinner never appears. On a slow load, the spinner appears after 100 ms and stays for at least 1 s, so it does not flash away.
Separate the parameters of one block with a semicolon, as in (after 100ms; minimum 1s).
Common mistakes
- Sub-blocks outside the defer chain.
@placeholder,@loadingand@errormust follow the closing brace of@deferdirectly. Other markup between them breaks the chain. - Different sizes per state. A tiny placeholder that is replaced by a tall component pushes content down the page. Give the placeholder dimensions close to the final content.
- Expecting the placeholder to return. After loading starts, the placeholder does not come back, even if a
whencondition later becomes false. - Heavy sub-blocks. Dependencies used only in
@loadingor@errorare not deferred with the main block. Keep these blocks light.
Exercise
A @defer (on viewport) block has a placeholder containing only the text "Loading soon". The block never loads when the user scrolls to it. What is the likely cause, and how do you fix it?
Show answer
The on viewport trigger observes the placeholder's single root element, and a text-only placeholder has none. Wrap the text in one element, such as <div>Loading soon</div>, or pass an explicit element reference to the trigger, such as on viewport(anchor).
Exercise
You write @loading (after 200ms; minimum 800ms). The chunk downloads in 150 ms. What does the user see during loading, and when does the main content appear?
Show answer
The loading block never appears, because the download finished before the 200 ms after delay. The main content renders as soon as the chunk is ready, since minimum only applies once the loading block has been shown.