Sub topic 1
Why components need to accept content
Prerequisites
This chapter assumes you can already do the following:
- Create an Angular component with the
@Componentdecorator and give it aselectorand atemplate. - Use a component inside another component's template by adding it to
imports. - Read a simple CSS selector such as
.card,[title], orh2. - Read basic TypeScript: classes, decorators, and generic types.
The examples use Angular 19 or later, where components are standalone by default. On older versions, add standalone: true to each @Component. Inputs use the signal-based input() function.
The problem: a component that knows too much
Suppose you build a card component that shows a title and a body. The first version most people write uses inputs:
import { Component, input } from '@angular/core';
@Component({
selector: 'app-simple-card',
template: `
<div class="card">
<h3>{{ title() }}</h3>
<p>{{ body() }}</p>
</div>
`,
})
export class SimpleCardComponent {
title = input('');
body = input('');
}This works while the body is plain text. Then the requests start:
- One card needs a bold word in the body.
- Another needs a list of links.
- A third needs a button that calls a method on the parent.
- A fourth needs a whole form.
Each request pushes you toward a worse design. You could add an html input and bind it with innerHTML, but that bypasses Angular's template compiler, so the markup cannot contain components, bindings, or event handlers. You could add an input per variation (showButton, linkList, formMode), but then the card has to know about every use of itself. The card is meant to be a reusable container, and it ends up depending on every place it is used.
The underlying issue is that inputs pass data, not structure. A card's job is to provide the frame (border, padding, header layout), and the caller should decide what goes inside.
Composition: the parent supplies the content
Composition means building a UI by nesting parts inside other parts. You already do this when you write <ul><li>...</li></ul>. The ul does not know what its li children contain. It only provides structure.
Angular lets your own components behave the same way. Instead of passing the card a string, the parent writes markup between the card's tags:
<app-card>
<p>Any markup can go <strong>here</strong>.</p>
<button (click)="save()">Save</button>
</app-card>This markup is called the card's content. The card has to say where that content should appear in its own template. It does this with an <ng-content> element. The act of taking content from the parent and rendering it inside the child is content projection.
The host element
Every component instance in the DOM sits inside a host element. It is the element that matches the component's selector. For <app-card> in a parent template, the browser DOM contains an actual app-card element. The component's template renders inside it.
So there are two different regions to keep apart:
- The component's own template: what the author of
CardComponentwrote intemplate. The card controls it. - The content: whatever the parent wrote between
<app-card>and</app-card>. The parent controls it.
Angular has to combine these two. The template says where the content goes, and <ng-content> is the marker for that place.
If the template has no <ng-content>, the content is not rendered at all. The parent can write as much as it likes between the tags, and none of it appears in the DOM. This is the most common first-day surprise with projection.
Your first ng-content
This is the smallest working projection component:
import { Component } from '@angular/core';
@Component({
selector: 'app-card',
template: `
<div class='card'>
<ng-content />
</div>
`,
})
export class CardComponent {}
// Usage in a parent template:
// <app-card><p>Hello from the parent</p></app-card>When the parent renders <app-card><p>Hello from the parent</p></app-card>, the DOM ends up like this:
<app-card>
<div class="card">
<p>Hello from the parent</p>
</div>
</app-card><ng-content> itself does not appear in the output. It is a placeholder that Angular replaces with the parent's nodes. Its self-closing form <ng-content /> is equivalent to <ng-content></ng-content>.
The diagram below shows how the pieces fit together.
A complete, runnable example
This example puts the card and a parent together, with a click handler that stays in the parent:
import { Component, signal } from '@angular/core';
@Component({
selector: 'app-card',
styles: `
:host { display: block; }
.card { border: 1px solid #ccc; border-radius: 6px; padding: 1rem; }
`,
template: `
<div class="card">
<ng-content />
</div>
`,
})
export class CardComponent {}
@Component({
selector: 'app-root',
imports: [CardComponent],
template: `
<app-card>
<p>You have saved {{ count() }} times.</p>
<button (click)="save()">Save</button>
</app-card>
`,
})
export class AppComponent {
count = signal(0);
save() {
this.count.update(n => n + 1);
}
}Three details are worth noticing:
- The
{{ count() }}binding and the(click)handler belong toAppComponent. The card knows nothing aboutcountorsave. It is still reusable for any other content. CardComponentmust be in theimportsof the component that uses it. If you forget, Angular reports thatapp-cardis not a known element.:host { display: block; }is needed because custom elements such as<app-card>aredisplay: inlineby default. An inline host with a block child inside it often gives odd layout behavior, so most container components setdisplay: blockon the host.
Inputs and projection solve different problems
Projection does not replace inputs. Most components use both.
| Need | Use |
|---|---|
| Pass a value such as a number, a string, or an object | Input |
| Let the parent choose a mode or a flag | Input |
| Let the parent supply arbitrary markup or components | Content projection |
| Let the parent supply markup the child renders several times or on demand | `ng-template` (covered in a later chapter) |
A good rule: if the thing you pass could sensibly be a JSON value, it is probably an input. If it is a piece of UI, it is probably content.
You can mix them freely. A card might take [elevated]="true" as an input to change its shadow and also project its body:
<app-card [elevated]="true">
<p>Body supplied by the parent.</p>
</app-card>How this connects to the rest of the topic
This chapter introduces the idea and the simplest slot. The following chapters build on it:
- Several slots chosen with the
selectattribute, and the default slot that catches everything else. ng-containerandngProjectAsfor grouping content without extra elements.- Fallback content for slots the parent leaves empty.
- Who owns the projected content, and why it is created even when its slot is not visible.
- Content queries (
contentChild,contentChildren) to read projected components. ng-templatewithngTemplateOutletwhen you want lazy or repeated content.
Keep one fact from this list in mind from the start: the parent creates and binds the projected nodes, not the child. That is why {{ count() }} above works without the card knowing anything about count.
Styling projected content
Style encapsulation affects projection. Angular scopes a component's styles to the elements in that component's own template. Projected nodes were written in the parent's template, so they carry the parent's scope, not the card's.
In practice:
- Rules in
CardComponentfor.cardaffect thedivin the card's template, because thatdivbelongs to the card. - A rule in
CardComponentsuch asp { color: red; }does not affect a<p>projected from the parent. The paragraph belongs to the parent's scope. - The parent's own styles do apply to the projected
<p>, because the parent wrote it.
If the card needs to style content it does not own, there are three common choices:
- Let the parent style its own content. This is the simplest.
- Use CSS custom properties, such as
--card-accent, that the card defines and the parent's content reads. - Use
::slotted-like behavior through:host ::ng-deep. This is deprecated and best avoided in new code.
Common mistakes
- Writing content but no
<ng-content>. The content disappears silently. If you see nothing between your tags, check the child's template first. - Forgetting
display: blockon the host. The layout looks fine in simple cases and breaks once you add margins or widths. - Using an input for a markup string. Binding
innerHTMLstrips Angular bindings and handlers, and it triggers sanitization. Use projection instead. - Expecting the child to read the parent's content by name. The child cannot reference
countorsave. If it needs to talk to the content, use content queries (later chapter) or an event or input contract. - Placing
<ng-content>more than once with noselect. Multiple unselected slots are a trap. Later chapters explain what happens, and the short version is that the content goes to only one of them.
Design thinking: what should the wrapper own?
Before writing a projection component, decide which parts belong to the wrapper and which belong to the caller:
- The wrapper owns the frame: borders, padding, layout, and consistent spacing.
- The wrapper owns behavior shared by all uses, such as a collapse toggle or a close button.
- The caller owns the meaning: the text, the list items, and the action buttons.
When this split is right, the wrapper rarely changes as new uses appear. When you keep adding inputs to a wrapper because a caller needs something slightly different, that is usually a sign the wrapper should have a slot instead.
Exercise
A teammate's app-banner component has the template <div class="banner"><h2>{{ heading() }}</h2></div>. A parent writes <app-banner heading="Hi"><button>Close</button></app-banner>. The button never appears. What is wrong, and what is the smallest change that fixes it?
Show answer
The banner's template has no <ng-content>, so Angular does not render anything the parent places between the tags. Add <ng-content /> inside the div, for example after the h2: <div class="banner"><h2>{{ heading() }}</h2><ng-content /></div>. The button then renders inside the banner, and its click handlers still belong to the parent.
Exercise
You are building a ProfileCard that always shows a user's name and a numeric reputation score, and lets each page add its own action buttons. Which parts should be inputs and which should be projected content? Give one reason.
Show answer
The name and the reputation score should be inputs, because they are plain data the card formats itself. The action buttons should be projected content. Each page needs different buttons with their own handlers, and projection lets the parent supply them without the card knowing about any of them.