Sub topic 2
Your first ng-content slot
This chapter assumes you can write a basic Angular standalone component, you know what a selector and a template are, and you can read TypeScript. You don't need anything about projection yet.
The problem a slot solves
A component's template is fixed at the time you write it. Suppose you want a card that draws a border and a shadow around whatever the caller puts inside it. If the template contains only the border, there is no place for the caller's markup to go.
An input could carry a string, but not arbitrary markup with its own components and bindings. Content projection solves this. The component declares a slot, and the parent fills it.
Declaring a slot
A slot is an <ng-content> element. It is a placeholder that Angular replaces with whatever sits between the component's opening and closing tags in the parent's template.
import { Component } from '@angular/core';
@Component({
selector: 'app-card',
template: `
<div class="card">
<ng-content />
</div>
`,
})
export class CardComponent {}ng-content is not a real DOM element. It never appears in the rendered page. Angular uses it only as a marker during compilation.
Filling the slot from a parent
The parent imports CardComponent and writes content between the tags.
import { Component } from '@angular/core';
import { CardComponent } from './card.component';
@Component({
selector: 'app-root',
imports: [CardComponent],
template: `
<app-card>
<p>Hello from the parent</p>
</app-card>
`,
})
export class AppComponent {}The rendered DOM looks like this:
<app-card>
<div class="card">
<p>Hello from the parent</p>
</div>
</app-card>The <app-card> tag stays in the DOM as the host element. The <div class="card"> comes from the card's template. The <p> comes from the parent, and Angular moved it to the spot where <ng-content /> was.
What can go inside
Projected content is not limited to text. You can pass any template syntax the parent can use: elements, other components, interpolation, event handlers, and control flow.
import { Component, signal } from '@angular/core';
import { CardComponent } from './card.component';
@Component({
selector: 'app-counter-page',
imports: [CardComponent],
template: `
<app-card>
<h2>Clicks: {{ count() }}</h2>
<button (click)="count.set(count() + 1)">Add one</button>
</app-card>
`,
})
export class CounterPageComponent {
count = signal(0);
}The {{ count() }} binding and the click handler refer to CounterPageComponent, not to CardComponent. The card has no count property and never needs one. Angular resolves projected content against the parent that wrote it. The chapter on ownership covers this in detail. For now, remember that the card only decides where the content goes, not what it does.
Styling and the host element
The host element <app-card> is an inline element by default. If your layout depends on it being a block, style it with :host.
import { Component } from '@angular/core';
@Component({
selector: 'app-card',
template: `
<div class="card">
<ng-content />
</div>
`,
styles: `
:host { display: block; }
.card { border: 1px solid #ccc; padding: 1rem; border-radius: 8px; }
`,
})
export class CardComponent {}Component styles are scoped to the component's own template. The .card rule applies to the div in the card's template, but it does not apply to the <p> the parent wrote, because the parent owns that element. If you need to style projected elements, put the styles in the parent.
Common errors
- Forgetting to import the component. If
CardComponentis missing from the parent'simportsarray, Angular reports thatapp-cardis not a known element. - Putting text in the wrong place. Content must sit between the opening and closing tags. Content placed after
</app-card>is not projected. - Expecting the slot to be a wrapper.
<ng-content />adds no element. If you need a wrapper for layout, add one in the component's template around the slot. - Self-closing the host tag. Writing
<app-card />is allowed in recent Angular versions, but it has no content to project.
Exercise
A parent writes <app-card><span>{{ title }}</span></app-card>. Which class must define title: the parent or CardComponent? Does <ng-content /> appear in the DOM?
Show answer
The parent must define title, because the parent wrote the span and Angular binds it in the parent's context. CardComponent does not need the property. <ng-content /> never appears in the DOM. The span is placed where the marker was, inside the card's div.