Sub topic 4
What lands in the default slot
This chapter assumes you can write a standalone Angular component and know the select attribute from the previous chapter. It covers the slot that receives everything your named slots do not claim.
The default slot
A default slot is an ng-content element with no select attribute. Angular also treats select="*" as a default slot. It collects every top-level node from the parent that no other slot's selector matched.
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 {}<app-panel>
<h2 panel-title>Settings</h2>
<p>This paragraph goes to the default slot.</p>
Plain text goes there too.
<button panel-actions>Save</button>
</app-panel>The <h2> goes to the header and the <button> goes to the footer. The <p> and the loose text both go to the <section>.
How Angular decides
Angular checks each top-level node between the component's tags against the select selectors. If a node matches one, it goes to that slot. If it matches none, it goes to the default slot.
flowchart TDBecause a fenced diagram needs its own block, here is the same decision as a diagram.
Two consequences follow from this.
- Order in the parent does not matter. Slot positions come from the child's template. You can write the button before the heading and it still lands in the footer.
- The default slot catches text and comments as well as elements. Text nodes have no attributes, so they can never match a selector.
Only top-level nodes are matched
Matching looks at the direct children of the component tag. It does not look inside them. This is the most common surprise with the default slot.
<app-panel>
<div class="wrapper">
<h2 panel-title>Settings</h2>
</div>
</app-panel>The <div> is the only top-level node, and it has no panel-title attribute. The whole <div>, including the heading inside it, goes to the default slot. The header stays empty.
You have two fixes. Move the attribute to the outer element, or remove the wrapper. If you need a wrapper that adds no element to the DOM, use ng-container with ngProjectAs, which the next chapter covers.
Control flow and structural directives
Wrapping projected content in @if or *ngIf does not change where the slot logic looks.
- With
*ngIfon an element, Angular still matches that element's own attributes, so<h2 panel-title *ngIf="show">reaches the header. - With an
@ifblock, Angular can match the block's content only when the block has a single root element. If the block holds several nodes, the whole block goes to the default slot.
<app-panel>
@if (show) {
<h2 panel-title>Settings</h2>
}
</app-panel>Here the block has one root element, so the heading reaches the header. Add a second element inside the block and the entire block moves to the default slot.
Several default slots
Use one default slot per component. If a template contains more than one ng-content without select, the projected nodes appear in only one of them, not copied into each. Do not depend on which one. If you need the same content in two places, use ng-template with ngTemplateOutlet, which a later chapter covers.
Common mistakes
| Symptom | Likely cause | Fix |
|---|---|---|
| Heading appears in the body, not the header | The attribute is on a nested element, not a top-level one | Move the attribute to the top-level node |
| Stray whitespace or text shows in the body | Loose text between the tags goes to the default slot | Remove it or accept it as body content |
| Content shows up nowhere | The component has no default slot and the node matched nothing | Add an `ng-content` without `select` |
The last row matters. If a component has only named slots, any unmatched content is silently dropped. Angular does not warn you. Add a default slot, even if you only use it for debugging.
Exercise
A component template contains <ng-content select='[card-header]' /> and <ng-content />. The parent writes <app-card><section><h2 card-header>Title</h2></section><p>Body</p></app-card>. Where does each element end up?
Show answer
The <section>, with the <h2> still inside it, goes to the default slot, because <section> is the top-level node and has no card-header attribute. The <p> also goes to the default slot. The header slot stays empty. To fix it, put card-header on the <section> or remove the wrapper.