Sub topic 6
Fallback content for empty slots
This chapter assumes you can write a standalone Angular component and that you know how ng-content and its select attribute route parent markup into slots. It covers what a slot shows when the parent supplies nothing for it.
The problem: an empty slot renders nothing
Suppose a component has a title slot. If the parent forgets to project a title, the slot is blank, and the layout around it may look broken:
import { Component } from '@angular/core';
@Component({
selector: 'app-section',
template: `
<h3><ng-content select="[section-title]" /></h3>
<ng-content />
`,
})
export class SectionComponent {}Used as <app-section><p>Body</p></app-section>, this renders an empty <h3>. You could force every parent to supply a title, but a default is friendlier.
Writing fallback content
Angular lets you put content between the opening and closing ng-content tags. That content is the fallback. It is shown only when the parent projects nothing into that slot.
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 {}Now a parent can use the component in two ways:
<app-section>
<span section-title>Billing</span>
<p>Invoices appear here.</p>
</app-section>
<app-section>
<p>No title was given, so the heading reads "Untitled section".</p>
</app-section>The first instance projects the span and ignores the fallback. The second matches nothing for [section-title], so the heading shows "Untitled section". Note that the self-closing form <ng-content /> cannot hold fallback content. You must use an explicit closing tag when you want a fallback.
Fallback content is supported in Angular 18 and later. In older versions the text between the tags is ignored.
Fallback belongs to the child
Projected content is owned by the parent, so its bindings use the parent's data. Fallback content is the opposite. It is written in the child's template, so it can use the child's inputs, fields, and other components.
import { Component, input } from '@angular/core';
import { DatePipe } from '@angular/common';
@Component({
selector: 'app-note',
imports: [DatePipe],
template: `
<article>
<ng-content />
<footer>
<ng-content select="[note-footer]">
Saved on {{ savedAt() | date: 'mediumDate' }}
</ng-content>
</footer>
</article>
`,
})
export class NoteComponent {
savedAt = input.required<Date>();
}<app-note [savedAt]="today">
<p>Remember to renew the license.</p>
</app-note>The footer shows "Saved on" followed by the formatted date. The fallback reads savedAt() from NoteComponent. If the parent adds an element with the note-footer attribute, that element replaces the default text.
Fallback is created even when hidden
Angular instantiates the fallback content whether or not it is displayed. Only the rendering is conditional. If your fallback contains a component with a constructor that makes an HTTP request, that request still runs when the parent supplies its own content.
Common mistakes
- Using the self-closing
<ng-content select="..." />and wondering why the default text never appears. - Expecting the fallback to appear when the parent projects an element that does not match the selector. An unmatched element goes to the default slot, so the named slot stays empty and shows its fallback, while the element appears elsewhere.
- Expecting the fallback to see parent variables. It only sees the child's own members.
Exercise
A component template contains <ng-content select="[icon]"><span>*</span></ng-content>. A parent writes <app-badge><img icon src="star.svg" /></app-badge>. Which markup does the slot show, and is the <span> created?
Show answer
The slot shows the img, because it matches [icon], so the fallback is not displayed. Angular still creates the <span> fallback content, but does not render it.
Exercise
Where does the expression {{ savedAt() }} get its value when it is written inside the fallback of an ng-content in NoteComponent: from the parent or from NoteComponent?
Show answer
From NoteComponent. Fallback content is part of the child's template, so it uses the child's members. Only projected content uses the parent's data.