Sub topic 2
Preventing Flicker with `minimum` and `after`
Assume you know Angular standalone components, template control flow, and the basics of JavaScript bundles. A deferred view loads its template dependencies later, so Angular must show a useful state while that loading happens.
Preventing flicker with `minimum` and `after`
@placeholder displays before Angular starts rendering the deferred content. minimum keeps it visible for a specified time. This prevents a very fast load from causing a brief flash.
@loading displays while the deferred chunk is loading. Its after parameter delays the loading state. If loading finishes before the delay, the learner never sees a spinner. Its minimum parameter keeps the spinner visible once it appears.
@defer (on viewport) {
<app-comments />
} @placeholder (minimum 500ms) {
<p>Comments will appear here.</p>
} @loading (after 100ms; minimum 1s) {
<app-spinner />
} @error {
<p>Comments could not be loaded.</p>
}Here, the placeholder remains for at least 500 milliseconds. Angular shows the loading block only when loading lasts longer than 100 milliseconds. Once shown, the loading block remains for at least one second.
How triggers decide when loading starts
The trigger determines when Angular requests the deferred chunk. After the chunk loads, Angular renders the main block. A trigger normally starts only once. A when condition, covered elsewhere, also does not undo a completed defer when its value later becomes false.
Time-based triggers
on idlestarts when the browser has idle time. It is the default trigger.on immediatestarts as soon as Angular finishes rendering the current view.on timer(500ms)starts after the specified delay.
@defer (on timer(2s)) {
<app-recommendations />
}User and viewport triggers
on viewportstarts when the block, or its placeholder, enters the viewport.on interaction(buttonRef)starts after interaction with a referenced element, such as a click or key event.on hover(areaRef)starts when the referenced element receives hover-like pointer focus.
<button #showChart type="button">Show chart</button>
@defer (on interaction(showChart)) {
<app-chart />
} @placeholder {
<p>Activate the button to load the chart.</p>
}Choose a viewport trigger for content below the fold. Choose interaction or hover for content opened by an obvious control. Use idle or immediate only when the deferred content is likely to be needed soon.
Exercise
Which configuration avoids showing a spinner for loads that finish within 150 milliseconds, while keeping it visible for at least one second after it appears?
Show answer
Use @loading (after 150ms; minimum 1s).