Discover how to make your Angular components magically accept any content you want without rewriting them!
Why ng-content for slot-based composition in Angular? - Purpose & Use Cases
Imagine building a reusable card component where you want to place different content like a title, image, and footer manually each time by editing the component's internal template.
Manually changing the component's template for every variation is slow, error-prone, and makes your code hard to maintain and reuse.
Using ng-content with slots lets you define placeholders inside your component where you can insert any content from outside, making your component flexible and reusable without changing its code.
<card> <h1 class="title">Title</h1> <img class="image" src="image.jpg"> <footer class="footer">Footer text</footer> </card>
<ng-content select=".title"></ng-content> <ng-content select=".image"></ng-content> <ng-content select=".footer"></ng-content>
You can create versatile components that accept different content in specific places, making your UI flexible and easier to manage.
A modal dialog component where you can insert a custom header, body, and footer content from outside without rewriting the modal itself.
Manually editing component templates for each use is inefficient.
ng-content slots let you insert external content into components.
This makes components reusable, flexible, and easier to maintain.
Practice
ng-content in Angular components?Solution
Step 1: Understand
ng-contentroleng-contentis used to project or insert external HTML content into a component's template.Step 2: Differentiate from other features
It does not define styles, handle events, or create services; those are unrelated to content projection.Final Answer:
To insert external content inside a component's template -> Option BQuick Check:
Content projection = Insert external content [OK]
- Confusing ng-content with event handling
- Thinking ng-content styles components
- Mixing ng-content with service creation
ng-content in Angular?Solution
Step 1: Recall correct attribute for named slots
Angular uses theselectattribute onng-contentto create named slots by CSS selectors.Step 2: Check each option
Only usesselectwith a CSS class selector, which is correct syntax.Final Answer:
<ng-content select=".header"></ng-content> -> Option AQuick Check:
Named slot uses select attribute [OK]
- Using name or slot attributes which Angular does not support
- Confusing with Web Components slot attribute
- Using id attribute instead of select
<div> <ng-content select=".title"></ng-content> <ng-content select=".body"></ng-content> </div>
And usage:
<my-comp> <h1 class="title">Hello</h1> <p class="body">Welcome!</p> </my-comp>
What will be rendered inside
<my-comp>?Solution
Step 1: Match content with selectors
Theng-content select=".title"inserts elements with classtitle, andselect=".body"inserts elements with classbody.Step 2: Check order of insertion
The template places.titlecontent first, then.bodycontent, so the output matches that order.Final Answer:
<div><h1 class="title">Hello</h1><p class="body">Welcome!</p></div> -> Option DQuick Check:
Content order follows ng-content slots [OK]
- Assuming order depends on usage, not template
- Ignoring CSS class selectors in select
- Expecting content without classes to appear
<div> <ng-content select="header"></ng-content> <ng-content select=".body"></ng-content> </div>
Usage:
<my-comp> <div class="header">Top</div> <div class="body">Content</div> </my-comp>
Solution
Step 1: Analyze the select attribute usage
The firstng-contentusesselect="header", which looks for elements named<header>, not classheader.Step 2: Compare with usage classes
The usage provides<div class="header">, which does not match the selectorheader(element selector). It should be.headerto match the class.Final Answer:
The selector 'header' misses the dot for class, so no content projects there -> Option CQuick Check:
Class selectors need dot prefix in select [OK]
- Using element name instead of class selector
- Thinking select cannot use class selectors
- Forgetting ng-content can be self-closing
ng-content to support this slot-based composition?Solution
Step 1: Identify correct attribute for named slots
Angular usesselectwith CSS selectors to define named slots inng-content.Step 2: Check each option's syntax
uses selectwith class selectors, matching Angular's pattern. Options B, C, and D use invalid selectors or attributes (name,slot) not supported by Angular.Step 3: Confirm semantic HTML usage
wraps slots in semantic elements (header,section,footer), improving accessibility and structure.Final Answer:
Option A's template correctly uses ng-content with select and semantic tags -> Option AQuick Check:
Use select with CSS classes for slots + semantic tags [OK]
- Using name or slot attributes instead of select
- Not using CSS selectors in select
- Ignoring semantic HTML for accessibility
Ready to prove it?
Pass the test and this pattern moves unknown
