> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# `@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](/guide/glossary.md#bundle).


## Defer until a condition is true

```typescript title="src/app/feed.ts"
import { Component, signal } from '@angular/core';
import { LYNX_ELEMENTS } from '@blotch/angular-lynx';
import { Comments } from './comments';

@Component({
  selector: 'app-feed',
  imports: [LYNX_ELEMENTS, Comments],
  template: `
    <view (bindtap)="showComments.set(true)">
      <text>Show comments</text>
    </view>

    @defer (when showComments()) {
      <app-comments />
    } @placeholder {
      <text>Tap to see comments</text>
    }
  `,
})
export class Feed {
  readonly showComments = signal(false);
}
```

- **Before the trigger fires**, Angular renders `@placeholder`. `<app-comments>` isn't created, so none of its native elements exist yet.
- **When `showComments()` becomes `true`**, Angular creates `<app-comments>` and removes the placeholder.
- **It stays created** after that, even if the signal turns `false` again.
- **Setting the signal in the tap handler is safe.** Angular creates the deferred view after the handler returns.

## Triggers

| Trigger           | Works on Lynx | Notes                                                                  |
| ----------------- | :-----------: | ---------------------------------------------------------------------- |
| `when condition`  |      Yes      | When the condition becomes `true`                                      |
| `on immediate`    |      Yes      | As soon as the template has rendered                                   |
| `on idle`         |      Yes      | When the app has a moment to spare. The default if you give no trigger |
| `on timer(500ms)` |      Yes      | After a delay                                                          |
| `on viewport`     |      Yes      | When the trigger element comes on screen, or nears it (`scrollMargin`) |
| `on interaction`  |      Yes      | When the trigger element is tapped                                     |
| `on hover`        |      Yes      | When a finger touches the trigger element, as in a phone's browser     |
| `prefetch …`      |   Not useful  | The code is already in the bundle, so there's nothing to fetch early   |

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

```typescript title="src/app/post.ts"
import { Component } from '@angular/core';
import { LYNX_ELEMENTS } from '@blotch/angular-lynx';
import { Comments } from './comments';
import { RelatedPosts } from './related-posts';

@Component({
  selector: 'app-post',
  imports: [LYNX_ELEMENTS, Comments, RelatedPosts],
  template: `
    @defer (on interaction) {
      <app-comments />
    } @placeholder {
      <view class="p-4">
        <text>Show comments</text>
      </view>
    }

    @defer (on viewport) {
      <app-related-posts />
    } @placeholder {
      <view class="h-[200px]">
        <text>Related posts</text>
      </view>
    }
  `,
})
export class Post {}
```

- **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 interaction`** fires when the trigger, or anything inside it, is tapped. Angular also listens for `keydown`, which iOS and Android never send.
- **`on hover`** fires 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](/guide/glossary.md#web-preview), the mouse moving over the trigger fires it.
- **`on viewport`** fires 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 [`lynxExposure`](/guide/exposure.md) directive** gives you the same signal as `on viewport` for any element, as `@defer (when area.visible())`.

## Load before it comes on screen

`on viewport` takes an [`IntersectionObserver`](/guide/exposure.md#thresholds-margins-and-a-root)'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:

```typescript title="src/app/article.ts"
import { Component } from '@angular/core';
import { LYNX_ELEMENTS } from '@blotch/angular-lynx';
import { RelatedPosts } from './related-posts';

@Component({
  selector: 'app-article',
  imports: [LYNX_ELEMENTS, RelatedPosts],
  template: `
    <scroll-view scroll-orientation="vertical" class="h-full">
      <text class="p-4">{{ body }}</text>

      @defer (on viewport({ scrollMargin: '300px' })) {
        <app-related-posts />
      } @placeholder {
        <view class="h-[200px]">
          <text>Related posts</text>
        </view>
      }
    </scroll-view>
  `,
})
export class Article {
  readonly body = 'A long article…';
}
```

- **`scrollMargin`** grows 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 in `px` or `%`, as CSS `margin` does.
- **`rootMargin`** grows only the screen, which the `<scroll-view>` still clips, so it doesn't load ahead inside one. Use `scrollMargin` there.
- **`threshold`** waits until that share of the trigger is on screen: `0.5` for half of it.
- **`trigger`** names 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

- **`@placeholder`** shows until the trigger fires. `@placeholder (minimum 500ms)` keeps it up for at least that long, to avoid a flash.
- **`@loading`** shows while Angular loads the deferred code. Nothing downloads on Lynx, so this takes almost no time and `@loading` shows only briefly, if at all. Add `minimum`, as in `@loading (minimum 500ms)`, if you want it visible. Otherwise leave it out.
- **`@error`** shows if loading fails. With the code already in the bundle, that's rare.

## Related

- [Detect when elements are visible](/guide/exposure.md)
- [Defer example](/examples/defer.md)
- [Angular's `@defer` guide](https://angular.dev/guide/templates/defer)
