Deferred Loading with @defer
Lazy-load template sections with @defer.
Deferred Loading with @defer is a free Angular Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Angular Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Deferred Loading with defer
The at-defer block lazily loads part of a template and its dependencies, improving initial load performance. The deferred content is split into a separate chunk.
Basic defer Syntax
Wrap content in an at-defer block. By default it loads when the browser is idle.
// @defer {
// <app-heavy-chart></app-heavy-chart>
// }Why Defer
Deferring heavy or below-the-fold components keeps the initial bundle small and the first paint fast. The component loads only when actually needed.
The placeholder Block
An at-placeholder block shows content before the deferred block is triggered.
// @defer {
// <app-comments></app-comments>
// } @placeholder {
// <p>Comments will load soon</p>
// }The loading Block
An at-loading block shows while the deferred chunk is being fetched.
// @defer {
// <app-comments></app-comments>
// } @loading {
// <p>Loading comments...</p>
// }The error Block
An at-error block renders if loading the deferred dependencies fails.
// @defer {
// <app-comments></app-comments>
// } @error {
// <p>Failed to load comments</p>
// }Triggers
You control when loading starts with triggers in an on clause, such as on idle, on viewport, on interaction, on hover, or on timer.
// @defer (on viewport) {
// <app-chart></app-chart>
// }on viewport
The on viewport trigger loads the content when it scrolls into view, perfect for below-the-fold sections.
on interaction and on hover
on interaction loads when the user clicks or focuses the placeholder; on hover loads when the pointer hovers over it.
// @defer (on interaction) {
// <app-editor></app-editor>
// } @placeholder {
// <button>Open editor</button>
// }The when Condition
You can also trigger on a boolean expression using when, loading once the expression becomes true.
// @defer (when shouldLoad()) {
// <app-details></app-details>
// }Prefetching
You can prefetch the deferred chunk separately from rendering, for example prefetch on idle, so it is ready before the render trigger fires.
// @defer (on viewport; prefetch on idle) {
// <app-chart></app-chart>
// }Quick Check: defer
Triggering deferred content.
Recap: Deferred Loading
You learned the at-defer block for lazy loading, with at-placeholder, at-loading, and at-error blocks, and triggers like on idle, on viewport, on interaction, on hover, on timer, and when, plus prefetching. You have completed the Built-in Control Flow course.
Frequently asked questions
Is the “Deferred Loading with @defer” lesson free?
Yes — the full text of “Deferred Loading with @defer” is free to read here on the web, and the Angular Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Angular Academy course, upgrade to CoddyKit PRO.
What will I learn in “Deferred Loading with @defer”?
Lazy-load template sections with @defer. You practise Angular Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Angular Academy?
No prior experience is required. Angular Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Deferred Loading with @defer” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Angular Academy lesson?
Yes. Every Angular Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Conditional Rendering with @if
- Looping with @for
- Switching with @switch
- Deferred Loading with @defer