Sub topic 2
What Lands in the Default Slot
This chapter assumes basic Angular components and templates, CSS selectors, and TypeScript. Content projection lets a child component render markup supplied by its parent.
What Lands in the Default Slot
A plain <ng-content> is the default slot. Angular places projected nodes there when they do not match any named slot.
import { Component } from '@angular/core';
@Component({
selector: 'app-panel',
template: `
<header><ng-content select="[panel-title]" /></header>
<main><ng-content /></main>
`,
})
export class PanelComponent {}<app-panel>
<h2 panel-title>Settings</h2>
<p>This paragraph matches the default slot.</p>
<button>Save</button>
</app-panel>The heading matches [panel-title]. The paragraph and button match no named selector, so they appear in <main>. A default slot is a catch-all, not a second copy of every projected node.
Grouping Content with `ng-container` and `ngProjectAs`
ng-container groups nodes without adding an element to the rendered DOM. ngProjectAs gives that group a static projection identity.
<app-panel>
<ng-container ngProjectAs="[panel-title]">
<h2>Settings</h2>
<small>Beta</small>
</ng-container>
<p>Panel body</p>
</app-panel>The container and its children are routed to the title slot. ngProjectAs is useful when adding a wrapper would change layout or styling. Its value is not a dynamic expression, so it cannot switch selectors at runtime.
Fallback Content for Empty Slots
Text inside an <ng-content> element is fallback content. Angular renders it when the parent supplies nothing for that slot.
@Component({
selector: 'app-section',
template: `
<h3>
<ng-content select="[section-title]">Untitled section</ng-content>
</h3>
<ng-content />
`,
})
export class SectionComponent {}Who Owns Projected Content
The parent owns projected content. Its bindings use the parent’s component context, even though the child template determines where the nodes appear.
Projected content is also created eagerly. Hiding its slot with an @if does not make expensive projected components lazy. Use ng-template with ngTemplateOutlet when creation must be delayed or repeated.
> callout note > Use ng-content for structural composition. Use ng-template when rendering time or repetition must be controlled.
Exercise
Where does a projected element go if it matches no named select slot?
Show answer
It goes to the child component’s plain, default <ng-content> slot.