Content Projection — Angular

Content projection shows HTML from a parent component inside a child component's template. <ng-content /> is a hole in the child's template. Everything the…

The menu card, the promo card and the order card on Jali's page share the same frame, but what's inside differs. An input can send text or a number to a child component. But an input is not a good way to send a whole piece of HTML, with its own heading, paragraph and button.

That's what content projection is for: HTML written by a parent component is shown inside a child component's template. The child component leaves a hole in its template. The parent component (the one that uses it) fills that hole with any markup (a piece of HTML) it likes. The hole is written <ng-content />. In other frameworks, a hole like this is called children (React) or <slot> (Vue). Picture Jali's pancake box. The box is always the same: logo, lid, string. What's inside changes, and the person who orders decides what goes in. A box with dividers even has dedicated spots: the name sticker on the lid, the pancake in the middle, the chilli sauce in a corner. The box is the child component, its contents are what the parent sends, the person who orders is the parent component, and each dedicated spot is a named hole.

ng-content: one hole

In this code, Card provides a frame with one hole, and App sends three elements into it:

import { Component } from '@angular/core'

@Component({
  selector: 'app-card',
  standalone: true,
  template: `
    <div class="card">
      <ng-content />
    </div>
  `,
  styles: `.card { border: 1px solid #ddd; border-radius: 12px; padding: 12px; }`,
})
export class Card {}

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [Card],
  template: `
    <app-card>
      <h3 title>Special Egg Pancake</h3>
      <p>Duck egg, beef.</p>
      <button action (click)="order()">Order</button>
    </app-card>
    <p>Ordered: {{ ordered }}</p>
  `,
})
export class App {
  ordered = 0

  order() {
    this.ordered = this.ordered + 1
  }
}

The DOM Angular builds:

<app-card>
  <div class="card">
    <h3 title="">Special Egg Pancake</h3>
    <p>Duck egg, beef.</p>
    <button action="">Order</button>
  </div>
</app-card>

The first exercise uses the same shape: a CardComponent with <div class="card"><ng-content /></div>. In the editor that card shows on its own, with no parent sending it content, so its .card is empty. What's graded is the shape of the template.

select: named holes

The select attribute on <ng-content> makes a named hole: a hole that only takes the elements that match what select holds. This is Card's template with three holes:

<!-- Card's template -->
<div class="card">
  <header><ng-content select="[title]" /></header>
  <div class="body"><ng-content /></div>
  <footer><ng-content select="[action]" /></footer>
</div>

The same content from App lands here:

Sent by App Lands in
<h3 title> <header>, because it matches [title]
<p> <div class="body">, the hole with no select
<button action> <footer>, because it matches [action]

The second exercise uses this pattern: <header><ng-content select="[header]" /></header>, <main><ng-content /></main> and <footer><ng-content select="[footer]" /></footer>.

ngProjectAs: passing a hole along

To send several elements to one named hole at once, wrap them all in <ng-container action>. ng-container is a wrapper that doesn't become an element in the DOM. Its action attribute is only used for matching [action].

It's a different problem when a component passes its own hole along to another component. In this example, MenuCard's template uses Card. The title MenuCard receives from App must still go into Card's header:

<!-- MenuCard's template -->
<app-card>
  <ng-content select="[title]" ngProjectAs="[title]" />
  <p>Stock: 6</p>
</app-card>

Angular matches the outermost element MenuCard sends to Card, which is the <ng-content>, not the <h3 title>. Without ngProjectAs, that <ng-content> has no title attribute, so App's <h3 title> lands in Card's plain hole, next to Stock: 6. ngProjectAs="[title]" (treat this as [title]) makes that <ng-content> count as matching [title].

📌 ng-template and fallback content

⚠️ Content that vanishes without a trace

TL;DR: Content projection shows HTML from a parent component inside a child component's template. <ng-content /> is a hole in the child's template. Everything the parent writes between the child component's opening and closing tags is moved into that hole. The content still belongs to the parent, (click) and {{ }} included. select="[title]" makes a named hole, which only takes outermost elements that have the title attribute. The rest goes to the hole without select. If there's no such hole, the rest is thrown away with no error. ngProjectAs="[title]" makes an element count as matching [title], mostly when a component passes its hole along to another component. ng-template is different: this mould doesn't show until it's used.

⚡ ng-content: a hole for sent content

Write:

@Component({
  selector: 'app-card',
  template: `<div class="card"><ng-content /></div>`,
})
export class Card {}
// used as: <app-card><h3>Special Egg Pancake</h3></app-card>

Meaning: <ng-content /> is a hole in Card's template. The parent component (the one that uses Card) writes the content between <app-card> and </app-card>, and Angular moves that content into the hole. The content still belongs to the parent, so a (click) in it calls the parent's method. Card only provides the frame.

In the first exercise, <ng-content /> must be inside <div class="card">, not next to it.

⚡ select: named holes

Write:

<header><ng-content select="[title]" /></header>
<div class="body"><ng-content /></div>
<footer><ng-content select="[action]" /></footer>
Guess first: select="[title]" makes a hole that only takes elements with a title attribute. If Card only has the [title] and [action] holes, and no plain hole (one without select), where does the parent's <p> go?

Nowhere. The <p> matches neither [title] nor [action], and there's no plain hole for it, so the <p> is thrown away: it doesn't show, and there's no error.

Meaning: A hole with select only takes the outermost sent elements that match its CSS selector. The rest goes to the plain hole, which is <div class="body"> in this code.

⚡ ng-content versus ng-template

Write:

<ng-template #promo><p>Today's promo</p></ng-template>

Meaning: ng-content shows what the parent component sends right away. ng-template only stores a mould. The <p>Today's promo</p> in this code doesn't show until the mould named #promo is used, for example through *ngTemplateOutlet="promo".

What you will learn