Sub topic 10
Lazy and repeated content with ng-template
This chapter assumes you know how ng-content slots work and that you can read basic Angular templates and signal-based input() declarations. It also assumes you know the earlier point that projected content is created eagerly.
The problem with ng-content
Angular creates projected content when the parent's template runs. It does this even if the child never puts the slot on screen. Suppose you wrap a heavy chart in a collapsed panel with ng-content. The chart's component is still constructed, its inputs are bound, and its change detection runs.
ng-content also renders each piece of content once. A child can't loop over a slot and stamp out one copy per item.
An ng-template fixes both problems. It is a block of markup that Angular compiles but does not create until someone asks for it.
Rendering a template on demand
The parent passes an ng-template as its content. The child finds it with a content query and renders it with ngTemplateOutlet.
import { Component, TemplateRef, contentChild, input } from '@angular/core';
import { NgTemplateOutlet } from '@angular/common';
@Component({
selector: 'app-lazy-box',
imports: [NgTemplateOutlet],
template: `
@if (open()) {
<ng-container [ngTemplateOutlet]="body()" />
}
`,
})
export class LazyBoxComponent {
open = input(false);
body = contentChild.required(TemplateRef);
}The parent uses it like this:
<app-lazy-box [open]="isOpen">
<ng-template>
<app-heavy-widget />
</ng-template>
</app-lazy-box>While isOpen is false, app-heavy-widget does not exist. When it becomes true, Angular creates the widget. When it goes back to false, @if destroys the widget.
contentChild.required(TemplateRef) returns the first ng-template in the projected content. It throws if none is found. Without .required, the query returns undefined when the template is missing.
Repeating content with a context
One template can be rendered many times, each time with different data. You pass the data through ngTemplateOutletContext. The template reads it with let- variables.
import { Component, TemplateRef, contentChild, input } from '@angular/core';
import { NgTemplateOutlet } from '@angular/common';
@Component({
selector: 'app-list',
imports: [NgTemplateOutlet],
template: `
<ul>
@for (item of items(); track item) {
<li>
<ng-container
[ngTemplateOutlet]="row()"
[ngTemplateOutletContext]="{ $implicit: item, index: $index }"
/>
</li>
}
</ul>
`,
})
export class ListComponent {
items = input.required<string[]>();
row = contentChild.required(TemplateRef);
}
@Component({
selector: 'app-root',
imports: [ListComponent],
template: `
<app-list [items]="fruits">
<ng-template let-fruit let-i="index">
{{ i + 1 }}. {{ fruit | uppercase }}
</ng-template>
</app-list>
`,
})
export class AppComponent {
fruits = ['apple', 'pear', 'plum'];
}The context object has two kinds of keys:
$implicitis the value that a barelet-fruitreceives.- Every other key is named in the template, as in
let-i="index", which reads theindexkey.
The child controls what data exists. The parent controls how it looks. This split is how many component libraries build list, table and menu components.
Scope of the template
Angular resolves bindings inside an ng-template in the scope where you wrote it. That scope is the parent, not the child that renders it. In the example above, fruits and the uppercase pipe come from the parent. The child supplies only the context variables. A template can therefore read parent fields, and it never needs access to the child's private state.
Choosing between ng-content and ng-template
| Need | Use |
|---|---|
| Static wrapper, content always shown | `ng-content` |
| Content that may never be shown, and is expensive | `ng-template` |
| Same markup rendered several times | `ng-template` with a context |
| Child needs to hand data back to the markup | `ng-template` with a context |
Common errors
- Writing
<ng-content>inside the template and expecting it to be lazy. It is still created eagerly. - Using
let-itembut passing the value under a different key than$implicit. The variable is thenundefined. - Forgetting to import
NgTemplateOutletin a standalone component. Angular reports an unknown property onng-container.
Exercise
A app-lazy-box wraps <app-heavy-widget /> in an ng-template, and open is false. Does the widget's constructor run? What changes if you replace the ng-template with plain projected content and an ng-content inside @if (open())?
Show answer
With the ng-template, the constructor does not run until open becomes true. With plain projected content, the parent creates the widget immediately, whether or not open is true. The @if around ng-content only controls whether the existing widget is attached to the page.