Flexible Components: Content Projection

Stop building rigid components. Learn how to inject HTML directly into your Angular components using ng-content and selectors.

Lesson ProgressStep 1 of 8
Initializing...
0 EXP

Welcome! Let's learn how to build super flexible components in Angular using Content Projection.

// Starting our journey into flexible components...

Understanding Content Projection

Content projection is a way to pass HTML content from a parent component to a child component. It allows you to build reusable UI shells (like cards, modals, or layouts) without hardcoding the content inside them.

Instead of passing data via attributes (`@Input`), you pass DOM elements.

Concept Check

Which HTML tag is used in the child component to mark where the content should go?

Advanced Architect Simulations

0 EXP

Aplica tus conocimientos en estas simulaciones avanzadas. ¡Todo nuestro contenido es gratuito!


Achievements

📦
Projection Apprentice

Successfully implement a basic single-slot ng-content.

🎯
Selector Sniper

Correctly map content to slots using CSS selectors.

🏗️
Layout Architect

Build a complex multi-slot component structure.

Mission: Multi-Slot Projection

Define a component template that uses ng-content with a select attribute to create a specific slot for a header (e.g., select=".header").

Architect AI Feedback:

> Awaiting template input...

Challenge: The Projection Lifecycle

Drag the steps into the correct logical order of how Content Projection works in Angular.

Parent: <app-child>Hello</app-child>
Child: <ng-content></ng-content>
Result: Hello appears in Child

Challenge: Complete the Footer Slot

Fill in the blanks to create a slot specifically for a footer element.

<=""></ng-content>

Consult the Architect

Angular Developers Hub

Mastering Content Projection in Angular: Beyond the Basics

In modern web development, reusability is king. You don't want to write a new Modal component for every single alert in your application. You want one ModalWrapper that can display a login form, a success message, or a video player depending on the context. In Angular, the key to unlocking this flexibility is Content Projection.

The Problem with @Input() for HTML

Beginners often try to pass content into components using @Input() strings.

// BAD PRACTICE
<app-card [title]="'My Title'" [body]="'Some long HTML string...'"></app-card>

This approach becomes messy quickly. You lose syntax highlighting, you can't easily include other components (like buttons or icons) inside the string, and it's hard to maintain. Content projection solves this by allowing you to write normal HTML between the opening and closing tags of your component.

Single-Slot vs. Multi-Slot Projection

Single-slot is the default behavior. If you place one <ng-content> tag in your component, Angular will take everything from inside the parent's component tags and dump it there.

Multi-slot projection allows for granular control. By adding a select attribute to <ng-content>, you create specific "drop zones".

Child Template

<header>
  <ng-content select=".card-header"></ng-content>
</header>
<main>
  <ng-content></ng-content>
</main>

Defines specific slots. Note: the second one has no selector, so it catches everything else.

Parent Usage

<app-card>
  <h1 class="card-header">Title</h1>
  <p>This goes to main.</p>
</app-card>

Content is distributed based on the class selector.

Advanced: The `ngProjectAs` Attribute

Sometimes, you want to wrap an element in a container (like an ng-container) for structural reasons, but you still want it to be projected into a specific slot. The ngProjectAs attribute allows you to "disguise" an element so it matches a selector.

<ng-container ngProjectAs="[card-title]">
  <h1>My Title</h1>
</ng-container>
Pro Tip: Content Projection happens during the component creation phase. This means the content is initialized before the view is checked. Use the ngAfterContentInit lifecycle hook if you need to access projected content programmatically using @ContentChild.

Angular Projection Glossary

ng-content
A placeholder element used in a component's template to specify where external content (provided by the parent) should be rendered.
Content Projection
The pattern (formerly known as Transclusion in AngularJS) of projecting nodes from one component's template into another.
select Attribute
An attribute on `<ng-content>` that takes a CSS selector (e.g., `.class`, `tag`, `[attr]`). It tells Angular which specific elements to project into that slot.
Multi-slot Projection
Using multiple `ng-content` tags with different `select` attributes to distribute content into different areas of the component layout.
ngProjectAs
An attribute that allows an element to match a projection selector even if the element itself (like an ng-container) doesn't natively match it.
@ContentChild
A decorator used to get a reference to the projected content within the component class logic.