Sub topic 1
Why components need to accept content
Prerequisites
This chapter assumes you know the following:
- How to write an Angular component with the
@Componentdecorator, including itsselectorandtemplate. - How to use a component in a parent template by writing its selector as a tag.
- Basic TypeScript.
- Basic CSS selectors, such as tag names and attribute selectors. They matter in later chapters.
The problem: components that fix their own content
Most components start out with a template that holds everything they show. Here is a card that displays a fixed message.
import { Component } from '@angular/core';
@Component({
selector: 'app-card',
template: `
<div class="card">
<p>Hello</p>
</div>
`,
})
export class CardComponent {}This card is not reusable. Every place that uses it shows "Hello". You could add an input() for the text, but inputs are a poor fit for markup. Suppose one card needs a heading and a list, and another needs a form. Passing that markup as strings would lose Angular's template features, such as bindings and directives.
The host element and what is between the tags
Every component instance has a host element. This is the element that matches the component's selector. For <app-card>, the host element is the app-card tag itself. The component's template renders inside it.
A parent can write content between the opening and closing tags of a component.
<app-card>
<p>Hello from the parent</p>
</app-card>By default Angular does not render that content. The component's template replaces whatever sits between the tags. The <p> above is simply discarded, because the card's template has no place for it.
Content projection
Content projection is the feature that lets a component render markup supplied by its parent inside its own template. The component declares a slot with the <ng-content> element. Angular places the parent's content in that slot.
You can think of a component with two kinds of input:
- Data, passed with
input()and bindings. - Content, passed between the tags and rendered where
<ng-content>appears.
A first look at the fix
Here is the card again, now with a slot.
import { Component } from '@angular/core';
@Component({
selector: 'app-card',
template: `
<div class="card">
<ng-content />
</div>
`,
})
export class CardComponent {}The parent can now decide what goes inside.
<app-card>
<p>Hello from the parent</p>
</app-card>Angular renders a div with the class card, and the <p> sits inside it. A second use of <app-card> can hold a form or a list. The card controls the frame: the border, the padding and the layout. The parent controls the content.
The next chapter covers this slot in detail. For now, what matters is the division of work.
When to project content and when to use inputs
Both mechanisms pass information into a component, so choosing between them is a design decision.
| Need | Better fit | Reason |
|---|---|---|
| A short value such as a label or a number | `input()` | The component can read, validate and format it. |
| Arbitrary markup, such as headings, lists or other components | Content projection | The parent writes normal template syntax. |
| A wrapper that adds layout or styling around anything | Content projection | The wrapper does not need to know what it wraps. |
| Data the component must compute with | `input()` | Projected markup is not data the component can read directly. |
Components that wrap other content are common. Cards, dialogs, panels, tab sets and layout shells are all typical examples.
A common mistake
A frequent mistake is to write content between the tags and see nothing on screen.
<app-banner>
<strong>Maintenance tonight</strong>
</app-banner>If BannerComponent has no <ng-content> in its template, the <strong> is not displayed. Angular does not report an error. If projected content is missing, first check that the child component's template contains an <ng-content>.
What comes next
The following chapters build on this idea. You will learn how a single slot works, how to route content into several named slots, and how to read projected components from the child. You will also learn who owns the projected content, which affects when it is created.
Exercise
A parent template contains <app-note><em>Remember</em></app-note>. The NoteComponent template is <div class="note"></div>. What does the user see, and what single change makes the <em> appear inside the div?
Show answer
The user sees an empty note, because the component's template has no slot and Angular discards the content between the tags. Adding <ng-content /> inside the div, giving <div class="note"><ng-content /></div>, makes the <em> appear inside it.
Exercise
Name one case where an input() is the better choice than content projection, and explain why.
Show answer
Passing a short value the component must read or compute with, such as a numeric max or a status string. An input gives the component the value as data, while projected markup cannot be read as data by the component.