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.
- input and state
- component and template
- rendered Angular UI
Explain the core concept and Angular data flow of Content Projection (ng-content)
Core syntax or pattern
// 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
- input and state
// card.component.tsContent Projection (ng-content) is used to pass content from a parent component into a child component.
- 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.
- rendered Angular UI
</app-card>Build a card with header, body, and action slots, testing optional actions, fallback title, and projected interactive content.
- input and state
- component and template
- 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.
// 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>(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
// 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
Quick quiz 2 / 3
Quick quiz 3 / 3
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