Thuta Learning
Angular
AdvancedWeb DevelopmentIntermediate

Content Projection (ng-content)

AngularLesson 4125 min

What you'll walk away with

  • Explain the core concept and Angular data flow of Content Projection (ng-content)
  • Run the example, predict its state and UI, and test edge and error cases
  • Apply a typed safe pattern in a real project and recognize common pitfalls

Explain it simply

Content Projection (ng-content), in plain language

Content Projection (ng-content) is used to pass content from a parent component into a child component.

Problem → Why → Solution

The problem — why this concept matters

Projected content can disappear when a slot selector does not match, so the markup contract must be clear.

A common mistake

One happy-path result does not prove the feature works; empty, loading, error, and cleanup behavior need explicit tests.

Core concept — understand how it works

Content projection places parent-authored markup into child layout slots to create reusable shells. Single and multiple slots, selectors, and fallbacks are available, but data context and style ownership remain important. It works well for cards and dialogs; dynamic template instantiation may instead call for template outlets.

Mental model
  1. input and state
  2. component and template
  3. rendered Angular UI

Explain the core concept and Angular data flow of Content Projection (ng-content)

Core syntax or pattern

typescript
// card.component.ts
@Component({
  selector: 'app-card',
  template: `
    <div class="card">
      <div class="card-header">
        <ng-content select="[header]"></ng-content>
      </div>
      <div class="card-body">
        <ng-content></ng-content>
      </div>
      <div class="card-footer">
        <ng-content select="[footer]"></ng-content>
      </div>
    </div>
  `
})
export class CardComponent { }

// Usage in parent
<app-card>
  <h2 header>Card Title</h2>
  <p>This is the main content of the card.</p>
  <button footer>Action</button>
</app-card>

Content projection places parent-authored markup into child layout slots to create reusable shells. Single and multiple slots, selectors, and fallbacks are available, but data context and style ownership remain important. It works well for cards and dialogs; dynamic template instantiation may instead call for template outlets.

Read the code step by step

  1. input and state// card.component.ts

    Content Projection (ng-content) is used to pass content from a parent component into a child component.

  2. component and template<ng-content select="[footer]"></ng-content>

    Content projection places parent-authored markup into child layout slots to create reusable shells. Single and multiple slots, selectors, and fallbacks are available, but data context and style ownership remain important. It works well for cards and dialogs; dynamic template instantiation may instead call for template outlets.

  3. rendered Angular UI</app-card>

    Build a card with header, body, and action slots, testing optional actions, fallback title, and projected interactive content.

Execution flow
  1. input and state
  2. component and template
  3. rendered Angular UI

Success

Explain the core concept and Angular data flow of Content Projection (ng-content)

Failure

Projected content can disappear when a slot selector does not match, so the markup contract must be clear.

Content Projection (ng-content) is used to pass content from a parent component into a child component.

typescript
// card.component.ts
@Component({
  selector: 'app-card',
  template: `
    <div class="card">
      <div class="card-header">
        <ng-content select="[header]"></ng-content>
      </div>
      <div class="card-body">
        <ng-content></ng-content>
      </div>
      <div class="card-footer">
        <ng-content select="[footer]"></ng-content>
      </div>
    </div>
  `
})
export class CardComponent { }

// Usage in parent
<app-card>
  <h2 header>Card Title</h2>
  <p>This is the main content of the card.</p>
  <button footer>Action</button>
</app-card>
You should see
(The content passed from the parent will show up in the card component's designated slots)

Real-world usage

How to apply it in a real project

Build a card with header, body, and action slots, testing optional actions, fallback title, and projected interactive content.

Mini challenge

Build it yourself

Rebuild the Content Projection (ng-content) example without copying it. Add two normal states, one edge case, and one failure state, and write the expected UI and state transition before running it.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
typescript
// card.component.ts
@Component({
  selector: 'app-card',
  template: `
    <div class="card">
      <div class="card-header">
        <ng-content select="[header]"></ng-content>
      </div>
      <div class="card-body">
        <ng-content></ng-content>
      </div>
      <div class="card-footer">
        <ng-content select="[footer]"></ng-content>
      </div>
    </div>
  `
})
export class CardComponent { }

// Usage in parent
<app-card>
  <h2 header>Card Title</h2>
  <p>This is the main content of the card.</p>
  <button footer>Action</button>
</app-card>

Expected: Explain the core concept and Angular data flow of Content Projection (ng-content)

Show hint

Build a card with header, body, and action slots, testing optional actions, fallback title, and projected interactive content.

Quick quiz 1 / 3

What best describes the core idea of Content Projection (ng-content)?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • input and state → component and template → rendered Angular UI
  • Content projection places parent-authored markup into child layout slots to create reusable shells. Single and multiple slots, selectors, and fallbacks are available, but data context and style ownership remain important. It works well for cards and dialogs; dynamic template instantiation may instead call for template outlets.
  • Projected content can disappear when a slot selector does not match, so the markup contract must be clear.
  • Build a card with header, body, and action slots, testing optional actions, fallback title, and projected interactive content.

Angular — Components — Angular

Easy traps

  • Projected content can disappear when a slot selector does not match, so the markup contract must be clear.
  • One happy-path result does not prove the feature works; empty, loading, error, and cleanup behavior need explicit tests.

Hands-on Exercise

Rebuild the Content Projection (ng-content) example without copying it. Add two normal states, one edge case, and one failure state, and write the expected UI and state transition before running it.

You'll know it worked when: (The content passed from the parent will show up in the card component's designated slots)

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Content Projection (ng-content) | Thuta Learning