AI Hints
Home›Angular Tutorial›Content Projection, Building a Small Component Tree and Practice: A Star Rating
Angular · Lesson 52 of 120

Content Projection

Ng-content.

⏱ 2 min read▶ Video at 1:01:19🎓 Free
Watch this lesson in the Angular courseWatch on YouTube ↗Full Angular course

Inputs pass data. But sometimes we want to pass a piece of HTML, like the body of a panel, or a dialog. That's content projection.

  • Inputs pass data
  • Sometimes, pass HTML
  • Content projection

ng-content

Whatever the parent puts between the child's tags is its content. The child shows that content wherever it places ng content. So the child provides the frame, and the parent fills it in.

keyvalue
Parent<app-panel> ...content... </app-panel>
Child<ng-content />
Child givesthe frame
Parent giveswhat goes inside

panel.ts

Panel draws a card, with a heading from its input. Ng content marks where the parent's content goes.

app.tstypescript
import { Component, input } from '@angular/core';

@Component({
  selector: 'app-panel',
  template: `
    <div class="card">
      <h3>{{ heading() }}</h3>
      <ng-content />
    </div>
  `,
})
export class Panel {
  heading = input.required<string>();
}

Two panels

The parent uses panel twice, with different content inside each one. The first has a sentence. The second has a sentence, and a button. Run it. Both panels share the same frame, but show their own content.

app.tstypescript
import { Component } from '@angular/core';
import { Panel } from './panel';

@Component({
  selector: 'app-root',
  imports: [Panel],
  template: `
    <app-panel heading="Opening hours">
      <p>Monday to Saturday, 9 to 5.</p>
    </app-panel>
    <app-panel heading="Sale">
      <p>All <b>Jane Austen</b> books, 20% off.</p>
      <button>See the sale</button>
    </app-panel>
  `,
})
export class App {}
Also uses panel.ts
import { Component, input } from '@angular/core';

@Component({
  selector: 'app-panel',
  template: `
    <div class="card">
      <h3>{{ heading() }}</h3>
      <ng-content />
    </div>
  `,
})
export class Panel {
  heading = input.required<string>();
}

Several slots

Ng content, with select, picks only matching content. The heading goes to the top, buttons go to the bottom, and everything else goes in the middle. The parent writes them in any order. Run it. The dialog put each piece in its own place.

app.tstypescript
import { Component } from '@angular/core';
import { Dialog } from './dialog';

@Component({
  selector: 'app-root',
  imports: [Dialog],
  template: `
    <app-dialog>
      <button>Remove</button>
      <p>Remove Dune from your basket?</p>
      <h3>Are you sure?</h3>
    </app-dialog>
  `,
})
export class App {}
Also uses dialog.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-dialog',
  template: `
    <div class="card">
      <ng-content select="h3" />
      <ng-content />
      <hr>
      <ng-content select="button" />
    </div>
  `,
})
export class Dialog {}

Summary

Ng content shows the parent's content inside the child, and select sends pieces to different slots. Next, building a small tree of components.

Key takeaways
  • <ng-content />
  • select="h3"
  • Child frame, parent content
Next up: Building a Small Component Tree — three levels