Sub topic 2
Your first ng-content slot
This chapter covers single-slot projection: how one <ng-content /> element turns a component into a reusable wrapper. It assumes you can write standalone Angular components with templates and selectors, and that you read basic TypeScript. Named slots, fallback content, and content queries come in later chapters. Here the goal is a precise model of what one slot does and does not do.
What a slot is
A component's template normally contains only markup that the component's author wrote. Content projection lets the parent add markup between the component's opening and closing tags, and lets the component decide where that markup appears. The place where it appears is called a slot, and you declare it with <ng-content />.
ng-content is not a real DOM element. The Angular compiler replaces it with the parent's nodes, and nothing named ng-content reaches the browser.
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;
border-radius: 8px;
padding: 1rem;
}
`,
})
export class CardComponent {}A parent uses it like this:
<app-card>
<p>Hello from the parent</p>
</app-card>The browser receives roughly this DOM:
<app-card>
<div class="card">
<p>Hello from the parent</p>
</div>
</app-card>The <p> was written in the parent's template, but it is placed inside the card's div. The card supplies the frame and the parent supplies the content.
How the pieces fit
Three things take part in projection:
- The host element is the
<app-card>element itself. It stays in the DOM and wraps the component's template. - The component template is the card's own markup, including the slot.
- The projected content is whatever sits between
<app-card>and</app-card>in the parent's template.
The slot is a position in the card's template. The parent's nodes are moved there when the view is built.
A worked example with bindings
Projected content can contain anything a normal template can: text, elements, other components, bindings, and event handlers. Here is a parent that projects a heading and a button into the card.
import { Component, signal } from '@angular/core';
import { CardComponent } from './card.component';
@Component({
selector: 'app-profile',
imports: [CardComponent],
template: `
<app-card>
<h2>{{ name() }}</h2>
<button (click)="rename()">Rename</button>
</app-card>
`,
})
export class ProfileComponent {
name = signal('Ada');
rename() {
this.name.set('Grace');
}
}Clicking the button calls rename() on ProfileComponent, and the heading updates. The card never sees name or rename. It has no inputs for them and does not need any. This is the main reason to use projection: the card stays unaware of what it displays, so it can wrap any content.
Compare this with an input-based design such as <app-card [title]="name()" />. An input is good for a value the component formats itself. Projection is better when the parent should control the markup, as with a heading level, a link, or a form.
The host element is part of your layout
Custom elements such as <app-card> default to display: inline. A block-level div inside an inline host usually behaves, but widths, margins, and flex or grid layouts that target the host can surprise you. That is why the card above sets :host { display: block; }. Set the display mode of the host deliberately in every wrapper component.
What one slot accepts
A bare <ng-content /> accepts all of the parent's content. This includes elements, text nodes, and components. It also includes content produced by control flow such as @if or @for written between the tags.
<app-card>
Plain text works too.
@if (showWarning()) {
<p class="warning">Check your settings.</p>
}
</app-card>If the component has no <ng-content /> at all, the content between its tags is not rendered. Angular does not report an error, so a wrapper that forgot its slot looks like an empty box.
Place the slot where the content should go, and add markup around it as needed:
import { Component } from '@angular/core';
@Component({
selector: 'app-alert',
template: `
<div class="alert" role="alert">
<strong>Notice:</strong>
<ng-content />
</div>
`,
})
export class AlertComponent {}<app-alert>Your session expires in five minutes.</app-alert>The result is the strong label followed by the parent's text, both inside the div.
Who styles projected content
This is the most common surprise with a first slot. Angular's default style encapsulation, called emulated, adds an attribute to every element that belongs to a component's template and scopes that component's styles to elements with the attribute. Projected nodes were written in the parent's template, so they carry the parent's attribute, not the card's.
import { Component } from '@angular/core';
@Component({
selector: 'app-card',
template: `<div class="card"><ng-content /></div>`,
styles: `
.card { padding: 1rem; }
p { color: crimson; } /* does NOT reach a projected <p> */
`,
})
export class CardComponent {}The p rule only matches paragraphs written in the card's own template. A projected <p> follows the parent's styles instead. The .card rule works because the div belongs to the card.
You have three reasonable options when a wrapper needs to style projected content:
- Let the parent style its own content. This is usually the cleanest choice.
- Use
:host ::ng-deep p { ... }. This works, but::ng-deepis deprecated in Angular, so avoid it in new code. - Expose CSS custom properties on the wrapper, such as
--card-text-color. The parent sets them and the card's styles read them. Custom properties inherit through the DOM, so they cross the projection boundary.
The card can style its own frame with .card, and it can pass inherited properties such as color and font-family down to projected content, because inheritance follows the DOM tree.
One slot, one projection
A bare <ng-content /> is a single destination, and the parent's nodes are projected only once. Placing the same unselected <ng-content /> twice does not render two copies of the content. The nodes exist once, so they can live in only one place.
import { Component } from '@angular/core';
@Component({
selector: 'app-twice',
template: `
<section><ng-content /></section>
<aside><ng-content /></aside> <!-- do not expect a second copy -->
`,
})
export class TwiceComponent {}Do not use this pattern to duplicate content. If the same content must appear in more than one place, or several times, project an ng-template and render it with ngTemplateOutlet. A later chapter covers that technique.
The same applies to <ng-content /> inside a @for loop. The loop does not repeat the projected nodes.
Slots inside conditionals
You can wrap the slot in control flow:
import { Component, input } from '@angular/core';
@Component({
selector: 'app-collapsible',
template: `
@if (open()) {
<div class="body"><ng-content /></div>
}
`,
})
export class CollapsibleComponent {
open = input(false);
}When open() is false, the content is not visible. That does not mean it was never created. Angular builds the parent's projected nodes when the parent's view is created, whether or not the slot is rendered, so components inside them are constructed and their bindings can run. The chapter on eager creation explains the consequences and the ng-template alternative. For now, remember that hiding a slot does not postpone the work of its content.
Common mistakes with a first slot
| Symptom | Likely cause | Fix |
|---|---|---|
| Content between the tags never appears | The component template has no `<ng-content />` | Add the slot where the content should render |
| Wrapper layout looks wrong | Host element is inline by default | Add `:host { display: block; }` or the display you need |
| Wrapper styles do not affect projected elements | Emulated encapsulation scopes styles to the component's own template | Style from the parent, or use CSS custom properties |
| Content appears in only one of two slots | Projected nodes exist once | Use `ng-template` with `ngTemplateOutlet` for repeats |
| Hidden content still runs its logic | Projected content is created by the parent regardless of the slot | Project an `ng-template` for lazy creation |
Design guidelines for single-slot wrappers
- Use one bare slot when the wrapper provides a frame, such as a card, a modal shell, or a container, and the content has no internal structure the wrapper must arrange.
- Keep the wrapper's template free of assumptions about what is projected. If you find yourself writing CSS like
:host > p, you probably want named slots, which the next chapter introduces. - Give the wrapper a descriptive selector and set the host display mode.
- Prefer inputs for simple values such as a label or a count, and projection for markup.
Exercises
Exercise
A teammate writes <app-card><p>Hi</p></app-card>, but the page shows an empty bordered box. The card's template is <div class="card"></div>. What is wrong, and how do you fix it?
Show answer
The template has no <ng-content />, so Angular has nowhere to render the content between the tags and silently drops it from the output. Add <ng-content /> inside the div: <div class="card"><ng-content /></div>.
Exercise
In app-card, you add the style p { color: crimson; }. A parent projects <p>Hello</p> into the card, but the text stays black. Why, and name two ways to get a crimson paragraph?
Show answer
With emulated encapsulation the card's styles apply only to elements in the card's own template, and the projected paragraph carries the parent's scope attribute. You can style the paragraph in the parent's own styles, or expose a CSS custom property such as --card-text-color that the parent sets and the paragraph's color reads. (::ng-deep also works but is deprecated.)
Exercise
A projected <h2>{{ name() }}</h2> is placed in a card. Which component's name signal does the binding read, the parent's or the card's, and why?
Show answer
The parent's. The heading was written in the parent's template, so the parent owns and binds it. The slot only decides where the node is placed in the DOM.