Sub topic 6
How triggers decide when loading starts
This chapter assumes you know standalone components, the imports array, and basic template syntax. It also assumes you have written a first @defer block. Here you will learn what a trigger is, which kinds exist, and how triggers combine.
What a trigger does
A @defer block does not render its content straight away. A trigger is the condition that tells Angular to start loading the block's code and then render it. If you write no trigger, Angular uses on idle.
There are two families of triggers:
ontriggers react to something the browser reports, such as idleness, visibility, or a click.whentriggers react to a boolean expression in your component.
You can list several triggers in one block, separated by semicolons. The block starts loading when any one of them fires. This is an OR relationship, not AND.
@defer (on viewport; on timer(5s)) {
<app-comments />
} @placeholder {
<div class="comments-placeholder"></div>
}In this block, loading starts when the placeholder scrolls into view, or after five seconds, whichever happens first.
The trigger catalog
The table lists every trigger covered in this tutorial. Later chapters go through them one by one.
| Trigger | Fires when | Needs a placeholder? |
|---|---|---|
| `on idle` | The browser is idle (default) | No |
| `on immediate` | Right after the page renders | No |
| `on timer(5s)` | The given time has passed | No |
| `on viewport` | The placeholder enters the viewport | Yes, or a reference |
| `on interaction` | The user clicks or presses a key on the target | Yes, or a reference |
| `on hover` | The pointer or focus reaches the target | Yes, or a reference |
| `when expr` | The expression first becomes true | No |
The user-driven triggers (viewport, interaction, hover) watch an element. By default that element is the root element of the @placeholder block. If there is no placeholder, you must name a target element with a template reference variable.
Triggers with a reference
Pass a reference variable to watch an element outside the block. The next example uses a button as the target.
<button type="button" #showChart>Show chart</button>
@defer (on interaction(showChart)) {
<app-chart />
} @placeholder {
<p>Press the button to see the chart.</p>
}The referenced element must be in the same template and must exist when the trigger is set up. Angular attaches its listener to that element.
The when trigger fires once
A when trigger starts loading the first time its expression becomes true. Angular does not undo the render if the expression later turns false.
@defer (when user.isPremium) {
<app-premium-panel />
}If user.isPremium flips from true back to false, <app-premium-panel /> stays on screen. Use @if around the block if you need content that appears and disappears.
Trigger lifecycle
The diagram shows what happens to a block over time. The first trigger to fire moves the block out of the placeholder state.
A block never goes back to the placeholder once it has been triggered. Each deferred block loads at most once.
Common mistakes
- Expecting AND logic.
@defer (on viewport; when ready)loads when either condition holds. To require both, write onewhenexpression that combines them, for examplewhen ready && isVisible. - Using a user trigger with no target.
on viewportwith no placeholder and no reference has nothing to watch, and Angular reports an error at compile time. - Referencing an element inside the deferred block. The trigger element must be outside the block, because the block's content does not exist yet.
- Expecting
whento reverse. The expression is checked until it first becomes true, then ignored.
Exercises
Exercise
A block is written as @defer (on hover(card); when isReady). The user never hovers over card, but isReady becomes true. Does the block load? What if isReady then becomes false?
Show answer
Yes, it loads when isReady becomes true, because multiple triggers are combined with OR and any one of them is enough. Setting isReady back to false does nothing, because when fires only once and the rendered block stays.
Exercise
You write @defer (on viewport) { <app-map /> } with no @placeholder and no reference variable. What is wrong, and how do you fix it?
Show answer
The viewport trigger has no element to observe. Either add a @placeholder block with a root element, or pass a reference variable, such as on viewport(mapAnchor) with <div #mapAnchor></div> placed outside the block.