@defer
@defer holds back part of a template until a trigger fires, so the first screen renders sooner. On Lynx it delays creating views, not downloading code: the deferred component's code is already in your bundle.
Defer until a condition is true
src/app/feed.ts
- Before the trigger fires, Angular renders
@placeholder.<app-comments>isn't created, so none of its native elements exist yet. - When
showComments()becomestrue, Angular creates<app-comments>and removes the placeholder. - It stays created after that, even if the signal turns
falseagain. - Setting the signal in the tap handler is safe. Angular creates the deferred view after the handler returns.
Triggers
On a device, on idle doesn't wait for idle time: Lynx has none to report. The block renders on the next turn of the event loop. A timeout such as on idle(500ms) (new in Angular 22) changes nothing there. The web preview waits for the browser's idle time, as on the web, or for the timeout if the browser stays busy.
Load when an element is tapped or comes on screen
src/app/post.ts
- The placeholder is the trigger element. It needs exactly one root element, here the
<view>. To use another element, give it a template reference variable and name it:<view #more>…</view>and@defer (on interaction(more)). on interactionfires when the trigger, or anything inside it, is tapped. Angular also listens forkeydown, which iOS and Android never send.on hoverfires when a finger touches the trigger, before it lifts. A phone's browser fires it for a tap too, since it sends mouse events for one. In the web preview, the mouse moving over the trigger fires it.on viewportfires when the trigger first appears on screen, for example when the user scrolls to it. Give the trigger a size: an element with no width or height never counts as on screen.- The
lynxExposuredirective gives you the same signal ason viewportfor any element, as@defer (when area.visible()).
Load before it comes on screen
on viewport takes an IntersectionObserver's options, except root. scrollMargin creates the block while its trigger is still that far from scrolling into view, so it's ready when the user gets there:
src/app/article.ts
scrollMargingrows the<scroll-view>or<list>the trigger is in, and the screen, by that much. Here Angular creates<app-related-posts>while its placeholder is still 300px below the bottom. It takes one to four values inpxor%, as CSSmargindoes.rootMargingrows only the screen, which the<scroll-view>still clips, so it doesn't load ahead inside one. UsescrollMarginthere.thresholdwaits until that share of the trigger is on screen:0.5for half of it.triggernames another element to watch, with the options:@defer (on viewport({ trigger: more, scrollMargin: '300px' })).- With options, the trigger is measured when it's laid out, when a scroll container around it is laid out again, and 20 times a second while one scrolls, rather than left to Lynx's own visibility checks, which can't see an element below a scroll-view's bottom.
The other blocks
@placeholdershows until the trigger fires.@placeholder (minimum 500ms)keeps it up for at least that long, to avoid a flash.@loadingshows while Angular loads the deferred code. Nothing downloads on Lynx, so this takes almost no time and@loadingshows only briefly, if at all. Addminimum, as in@loading (minimum 500ms), if you want it visible. Otherwise leave it out.@errorshows if loading fails. With the code already in the bundle, that's rare.
