Sub topic 6
Fallback content for empty slots
Why slots need a default
A content projection slot is an <ng-content> element in a component's template. The parent fills it with markup placed between the component's tags. Sometimes the parent has nothing to put there. A card with no title, a dialog with no footer, or a panel with no icon can each leave a hole in the layout.
You can handle that hole in three ways:
- Make the parent always supply content. This pushes repetitive work onto every consumer.
- Render the surrounding markup conditionally. This usually needs extra inputs or content queries.
- Give the slot fallback content: default markup that appears only when the parent projects nothing into it.
This chapter covers the third option. It assumes you can already build single-slot and multi-slot components with select, and that you know the default slot catches everything the named slots do not.
The syntax
Fallback content is whatever you write between the opening and closing <ng-content> tags. Angular supports this from version 18.
import { Component } from '@angular/core';
@Component({
selector: 'app-section',
template: `
<h3><ng-content select='[section-title]'>Untitled section</ng-content></h3>
<ng-content />
`,
})
export class SectionComponent {}If the parent writes <app-section><p>Body</p></app-section>, nothing matches [section-title]. The heading renders "Untitled section". The paragraph goes to the default slot.
If the parent writes <app-section><span section-title>Billing</span><p>Body</p></app-section>, the heading renders "Billing". The "Untitled section" text is not created at all.
The rule is all or nothing per slot. Angular never mixes projected content with fallback content in the same slot. One matching node in the slot is enough to suppress the fallback entirely.
What counts as empty
A slot is empty when no node from the parent was matched to it. The details matter, because "looks empty" and "is empty" differ.
| Parent writes | Slot treated as |
|---|---|
| Nothing between the tags | Empty, fallback shows |
| Only whitespace or line breaks | Empty, fallback shows (Angular removes whitespace-only text nodes by default) |
| An element matching the slot's selector | Filled |
| Text that is not whitespace | Filled, for the default slot |
| An `ng-container` with a matching `ngProjectAs` | Filled |
| A control flow block (`@if`, `@for`) that renders no output | Not reliably empty (see the pitfall below) |
The whitespace row is why this works in a typical formatted template:
<app-section>
</app-section>The line break between the tags is not content, so the heading still shows "Untitled section".
The default whitespace handling comes from the compiler option preserveWhitespaces, which is false unless you turn it on. If a project sets it to true, test your empty cases again.
The decision at render time
The following diagram shows how Angular decides, slot by slot, which content to render.
Matching follows the same rules as always. Named slots claim their matching nodes first. The default slot takes the rest. Fallback only comes into play after that assignment is done.
Fallback content belongs to the child
Projected content is created and bound by the parent. Fallback content is the opposite: the component that declares the <ng-content> owns it. Its template expressions run against the component's own class, not the parent's.
That makes fallback content a good place for defaults that depend on the component's inputs.
import { Component, input } from '@angular/core';
@Component({
selector: 'app-message-box',
template: `
<div class='box'>
<strong><ng-content select='[box-title]'>{{ defaultTitle() }}</ng-content></strong>
<ng-content>No message.</ng-content>
</div>
`,
})
export class MessageBoxComponent {
defaultTitle = input('Notice');
}A parent can use it in two ways:
import { Component } from '@angular/core';
import { MessageBoxComponent } from './message-box.component';
@Component({
selector: 'app-demo',
imports: [MessageBoxComponent],
template: `
<app-message-box />
<app-message-box defaultTitle='Heads up'>
<p>Disk almost full.</p>
</app-message-box>
`,
})
export class DemoComponent {}The first box shows "Notice" and "No message." because both slots are empty. The second box shows "Heads up" and the paragraph. The [box-title] slot is still empty, so its fallback renders, and the fallback reads the defaultTitle input. The default slot is filled, so "No message." is gone.
Because the child owns the fallback, the fallback can do anything a template can do:
- Use interpolation and property bindings against the component's members.
- Include other components, as long as the child imports them.
- Use control flow such as
@ifand@for. - Contain its own nested
<ng-content>. Check this against your Angular version before relying on it.
Fallback and ngProjectAs
Grouping with ng-container and ngProjectAs interacts with fallback in a predictable way. The ng-container is projected as a unit, and its ngProjectAs value decides which slot it matches. A matching container fills the slot, even if its contents are small.
<app-panel>
<ng-container ngProjectAs='[panel-title]'>
<h2>Settings</h2>
<small>beta</small>
</ng-container>
<p>Body text</p>
</app-panel>A panel whose title slot has a fallback would not show that fallback here, because the container matched [panel-title].
An empty container still counts as a match. Suppose you write <ng-container ngProjectAs='[panel-title]'></ng-container> to produce no title. The fallback will still be suppressed, and the slot will render nothing. Angular decides emptiness by whether a node matched, not by whether the visible output is blank.
Pitfall: conditional content fills the slot
The most common surprise is projecting content that sometimes renders nothing.
<app-section>
@if (showTitle) {
<h2 section-title>Billing</h2>
}
<p>Body</p>
</app-section>A natural expectation is that showTitle = false makes the slot empty, so "Untitled section" appears. In current Angular versions, you should not rely on that. The @if block is itself a node in the parent's template. It is projected as a unit, and a block with a single root element is matched using that element's attributes. When the condition is false, the block exists but renders nothing, and the slot is still treated as filled. The result can be an empty heading with no fallback.
The same applies to *ngIf on a projected element, @for over an empty list, and any other structural wrapper. Behavior around these details has changed between releases, so test your case in the version you ship.
There are two reliable fixes.
- Move the decision into the child. Give the component an input and let it choose what to render.
- Omit the element from the parent's template entirely rather than wrapping it in a conditional.
The first fix looks like this:
import { Component, input } from '@angular/core';
@Component({
selector: 'app-titled-section',
template: `
<h3>
@if (title(); as t) {
{{ t }}
} @else {
Untitled section
}
</h3>
<ng-content />
`,
})
export class TitledSectionComponent {
title = input<string>();
}Plain data such as a string works well as an input. Use fallback content for slots that carry markup, such as icons, buttons, or richly formatted text.
Pitfall: fallback does not hide the need for a wrapper
Fallback replaces the contents of a slot. It does not remove markup around the slot. In the earlier app-section, the <h3> always renders, even if the parent supplies a title with no visible text. If an empty slot should hide its wrapper, the component needs a way to know the slot is empty. That is a case for a content query, which a later chapter in this plan covers. Fallback content cannot report whether it was used.
Design guidelines
- Use fallback for sensible, harmless defaults: a placeholder title, a default icon, a generic "No items" message.
- Do not use fallback to hide missing required content. If a slot must be filled, say so in the component's documentation.
- Keep fallback markup small. If it grows into a full template, consider an input or a separate component.
- Keep styling consistent. Fallback content lives in the child's template, so the child's component styles apply to it directly. Projected content keeps the parent's styles.
- Test both cases, empty and filled, for every slot that has fallback.
Fallback in a multi-slot component
Here is a complete component that combines named slots, a default slot, and fallback in two places.
import { Component } from '@angular/core';
@Component({
selector: 'app-dialog',
template: `
<div class='dialog'>
<header><ng-content select='[dialog-title]'>Confirm</ng-content></header>
<section><ng-content /></section>
<footer>
<ng-content select='[dialog-actions]'>
<button type='button'>OK</button>
</ng-content>
</footer>
</div>
`,
})
export class DialogComponent {}These usages produce different results:
| Usage | Header | Body | Footer |
|---|---|---|---|
| `<app-dialog />` | Confirm | empty | OK button |
| `<app-dialog><p>Delete this?</p></app-dialog>` | Confirm | Delete this? | OK button |
| `<app-dialog><h2 dialog-title>Delete</h2><button dialog-actions>Yes</button></app-dialog>` | Delete | empty | Yes button |
The body slot has no fallback, so it is simply blank when nothing is supplied. Giving it one would be a design choice. For a body, an empty dialog may signal a bug, which is a reason to leave the slot without a default.
Exercise
A component template contains <h4><ng-content select='[badge]'>New</ng-content></h4>. A parent writes <app-item>@if (isNew) { <span badge>Hot</span> }</app-item> and isNew is false. Why might the heading come out empty instead of showing "New", and how would you make the fallback appear reliably?
Show answer
The @if block is a node in the parent's template and is projected as a unit. It can match the [badge] slot even when its condition is false, so the slot counts as filled and the fallback is not rendered. To get a reliable result, move the condition into the child (for example a badge input that the child checks with @if ... @else to show "New"), or leave the @if out of the parent and omit the element when it is not needed. Test in the Angular version you use, because projection of control flow blocks has changed between releases.
Exercise
In <ng-content select='[box-title]'>{{ defaultTitle() }}</ng-content>, which class does defaultTitle() resolve against, the parent's or the child's, and why?
Show answer
The child's. Fallback content is part of the child component's own template, so the component that declares the ng-content owns it. Projected content, by contrast, is created and bound by the parent.