ng-content lets you insert content from outside a component into specific places inside it. This helps build flexible and reusable components.
ng-content for slot-based composition in Angular
<ng-content></ng-content>
<!-- For named slots -->
<ng-content select=".slot-name"></ng-content>The plain <ng-content> tag inserts all content passed inside the component.
Using select attribute lets you create named slots to place specific content in specific spots.
<ng-content></ng-content>
header in the first slot and content with class body in the second slot.<ng-content select=".header"></ng-content> <ng-content select=".body"></ng-content>
<h1> elements in the first slot and all <p> elements in the second slot.<ng-content select="h1"></ng-content> <ng-content select="p"></ng-content>
This example shows a CardComponent with three slots: header, body, and footer. The AppComponent passes content with matching classes into each slot. The card displays the content in the right places.
import { Component } from '@angular/core'; @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 select=".body"></ng-content> </div> <div class="card-footer"> <ng-content select=".footer"></ng-content> </div> </div> `, styles: [ `.card { border: 1px solid #ccc; padding: 1rem; border-radius: 0.5rem; max-width: 300px; } .card-header { font-weight: bold; margin-bottom: 0.5rem; } .card-body { margin-bottom: 0.5rem; } .card-footer { font-size: 0.8rem; color: gray; }` ] }) export class CardComponent {} @Component({ selector: 'app-root', template: ` <app-card> <div class="header">My Card Title</div> <div class="body">This is the main content inside the card.</div> <div class="footer">Footer info here</div> </app-card> ` }) export class AppComponent {}
Content inside ng-content is projected from the parent component.
If no matching content is passed for a named slot, that slot stays empty.
You can combine multiple ng-content tags to create complex layouts.
ng-content lets you insert external content inside your component.
Use select to create named slots for different content parts.
This helps build reusable and flexible components that accept custom content.
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
