Sub topic 4
What lands in the default slot
This chapter assumes you can write a standalone Angular component, you know that <ng-content /> projects the parent's markup, and you have seen select route content into named slots. Here you will learn exactly which content ends up in the default slot, which content never reaches it, and the mistakes that follow from that.
The default slot is the catch-all
An <ng-content /> element with no select attribute is the default slot. It receives every piece of projected content that no other slot claimed. The component author does not list what it accepts. It accepts everything left over.
import { Component } from '@angular/core';
@Component({
selector: 'app-panel',
template: `
<header><ng-content select="[panel-title]" /></header>
<section><ng-content /></section>
<footer><ng-content select="[panel-actions]" /></footer>
`,
})
export class PanelComponent {}The parent decides what goes where by what it writes between the app-panel tags:
import { Component } from '@angular/core';
import { PanelComponent } from './panel.component';
@Component({
selector: 'app-root',
imports: [PanelComponent],
template: `
<app-panel>
<h2 panel-title>Settings</h2>
Plain text goes to the default slot.
<p>So does this paragraph.</p>
<button panel-actions>Save</button>
<span>And this span.</span>
</app-panel>
`,
})
export class AppComponent {}The <h2> goes to the header and the <button> goes to the footer. The text, the <p> and the <span> all go into the <section>, in the order they appear in the parent's template. Slot assignment does not change relative order inside a slot.
Only direct children are matched
Angular decides the slot for each top-level child of the component tag, meaning each node written directly between <app-panel> and </app-panel>. It never looks inside those children. A descendant travels with its ancestor.
<app-panel>
<div class="wrapper">
<h2 panel-title>Settings</h2>
</div>
</app-panel>Here the <div> is the top-level child. It has no panel-title attribute, so it goes to the default slot, and the <h2> goes with it. The header stays empty. This is the most common surprise with the default slot: a wrapper element hides the attribute that would have matched.
You have two ways to fix it:
- Remove the wrapper and put the attribute on a top-level element.
- Keep a grouping wrapper, but use
<ng-container ngProjectAs="[panel-title]">so no extra element appears in the DOM.
<app-panel>
<ng-container ngProjectAs="[panel-title]">
<h2>Settings</h2>
<small>beta</small>
</ng-container>
</app-panel>A bare <ng-container> has no attributes, so without ngProjectAs it always lands in the default slot.
What counts as a top-level node
The following table lists the kinds of top-level node you will write and where each one goes, for a component with the slots above.
| What the parent writes | Where it goes |
|---|---|
| Element whose tag, attribute or class matches a `select` | That named slot |
| Element that matches no `select` | Default slot |
| Text, including `{{ interpolation }}` | Default slot |
| `<ng-container>` without `ngProjectAs` | Default slot |
| `<ng-container ngProjectAs="[panel-title]">` | The slot matching that selector |
| Element with `*ngIf` or `*ngFor` and a matching attribute | The matching slot, because the element's own attributes are used |
| Descendant of a top-level element | Wherever its top-level ancestor goes |
Text can never match a select, because a selector describes an element. Any text you write directly inside the component tag goes to the default slot. This includes the whitespace between elements, though Angular strips most of it at compile time unless you turn on preserveWhitespaces.
Matching uses static information
The compiler matches select selectors against the tag name, the attributes and the classes written in the template. A class added by a binding such as [class.title]="flag" is not visible to the match, and neither is a class added later in code. If the parent writes
<app-panel>
<h2 [class.panel-title]="true">Settings</h2>
</app-panel>and the component uses select=".panel-title", the heading goes to the default slot. Use a static attribute or class for anything that must route to a named slot.
Control flow blocks
Blocks such as @if and @for are also top-level nodes. In Angular 17 and later, a block whose content is a single root element is matched using that element, so @if (ready) { <h2 panel-title>Settings</h2> } goes to the title slot. A block with more than one root node, or with text mixed in, goes to the default slot. If you need such a block in a named slot, wrap its content in <ng-container ngProjectAs="[panel-title]"> or restructure it so there is one root element.
How the decision is made
Each top-level node is compared against the select selectors in the component's template. The first slot that matches wins. If none matches, the node goes to the default slot.
Because the diagram fence must follow the required format, here it is in full:
When there is no default slot
A component is not required to have a default slot. If every <ng-content> has a select, nodes that match nothing have nowhere to go. Angular does not report an error. The nodes are simply not rendered.
import { Component } from '@angular/core';
@Component({
selector: 'app-strict-panel',
template: `
<header><ng-content select="[panel-title]" /></header>
<footer><ng-content select="[panel-actions]" /></footer>
`,
})
export class StrictPanelComponent {}With this component, <app-strict-panel><p>Hello</p></app-strict-panel> shows nothing for the paragraph. This can be a deliberate way to enforce a strict layout, but it fails silently, so a typo in an attribute name makes content vanish. The paragraph is still created and bound by the parent even though it is not in the DOM. A later chapter covers that eager creation.
Only one default slot receives content
You may write two <ng-content /> elements without select. Angular does not copy the content into both. The nodes are projected into only one of them (in practice the last), and the other stays empty. If you need the same content in two places, accept an <ng-template> and render it twice with ngTemplateOutlet. A single default slot is the clear and reliable design.
Default slot with fallback content
Fallback content works in the default slot as it does in named slots. It appears only when no top-level node is projected there:
import { Component } from '@angular/core';
@Component({
selector: 'app-section',
template: `
<h3><ng-content select="[section-title]">Untitled section</ng-content></h3>
<ng-content>Nothing to show yet.</ng-content>
`,
})
export class SectionComponent {}<app-section></app-section> renders both fallback strings. Adding <p>Body</p> replaces only the second one. Whitespace-only content that Angular strips counts as empty, so a line break inside the tags does not suppress the fallback. A projected comment or an empty <ng-container> does count as content and suppresses it, so avoid leaving those in the parent when you want the fallback to appear.
Designing a good default slot
The default slot suits the component's main body: card content, dialog text, a list's items. Named slots suit parts with a fixed position, such as a title or an action bar.
- Give the default slot the content most callers will have, so the simplest usage needs no attributes.
- Choose attribute selectors such as
[panel-title]over tag selectors, because they work with any element and do not collide with real tags. - Document each named slot. Callers cannot see slots in the type system, so a misspelt attribute fails silently by falling into the default slot.
- Prefer a default slot to a strict component with no catch-all when you want to be forgiving. Prefer no default slot when stray content would break the layout, and accept that mistakes will not raise errors.
Summary of rules
- An
<ng-content>with noselectis the default slot. - Only top-level children of the component tag are matched. Descendants go with their ancestor.
- Text, unmatched elements and bare
<ng-container>elements go to the default slot. - Matching uses static tag, attribute and class information from the template.
- Without a default slot, unmatched content is created but not displayed.
Exercise
A parent writes <app-panel><section><button panel-actions>Save</button></section></app-panel>. The panel has a slot <ng-content select="[panel-actions]" /> and a default slot. Where does the button appear, and how do you make it appear in the actions slot without adding a DOM element?
Show answer
The button appears in the default slot. The top-level child is the <section>, which matches no selector, and the button travels with it. To fix it, remove the <section> wrapper so the button is top-level. Alternatively, replace the <section> with <ng-container ngProjectAs="[panel-actions]">, which puts the button in the actions slot without adding a DOM element.
Exercise
A component has <ng-content select=".title" /> and no default slot. The parent uses <h2 [class.title]="true">Hi</h2>. What is rendered, and why?
Show answer
Nothing is rendered for the heading. Slot matching uses the static classes and attributes in the template, and a [class.title] binding is not visible to it. The heading matches no slot. The component has no default slot, so the heading is created but not displayed. Writing class="title" statically fixes it.