Bird
Raised Fist0
Angularframework~10 mins

ng-content for slot-based composition in Angular - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - ng-content for slot-based composition
Parent Component Template
↓
Uses <ng-content> in Child
↓
Angular Projects Parent Content
↓
Content Rendered in Child Slots
↓
Final Rendered DOM with Parent Content in Child
Angular takes the content inside a parent component's tag and inserts it into the child component where <ng-content> tags are placed.
Execution Sample
Angular
/* Parent template */
<child>
  <h1>Title</h1>
  <p>Paragraph</p>
</child>

/* Child template */
<div>
  <ng-content></ng-content>
</div>
Parent passes HTML content to child, which displays it inside its <ng-content> placeholder.
Execution Table
StepActionParent ContentChild TemplateResulting Render
1Parent renders <child> with inner HTML<h1>Title</h1><p>Paragraph</p><div><ng-content></ng-content></div>Child component placeholder ready
2Angular finds <ng-content> in child<h1>Title</h1><p>Paragraph</p><div><ng-content></ng-content></div>Prepare to insert parent content
3Angular projects parent content into <ng-content><h1>Title</h1><p>Paragraph</p><div><ng-content></ng-content></div><div><h1>Title</h1><p>Paragraph</p></div>
4Browser renders final DOM<h1>Title</h1><p>Paragraph</p><div><ng-content></ng-content></div>Visible: Title and Paragraph inside child div
💡 All parent content inserted into child at <ng-content>, rendering completes.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
Parent ContentEmpty<h1>Title</h1><p>Paragraph</p><h1>Title</h1><p>Paragraph</p><h1>Title</h1><p>Paragraph</p>
Child Template<div><ng-content></ng-content></div><div><ng-content></ng-content></div><div><ng-content></ng-content></div><div><h1>Title</h1><p>Paragraph</p></div>
Rendered DOMEmptyEmptyEmpty<div><h1>Title</h1><p>Paragraph</p></div>
Key Moments - 2 Insights
Why does the content inside <child> appear inside the child component's template?
Because Angular replaces the <ng-content> tag in the child template with the content placed inside the <child> tag in the parent template, as shown in execution_table step 3.
What happens if the child component template has multiple <ng-content> tags?
Angular can project different parts of the parent content into different <ng-content> slots using selectors, but in this example with one <ng-content>, all content goes there (see execution_table step 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What does Angular do at this step?
AIt duplicates the child template.
BIt removes the parent content from the DOM.
CIt inserts the parent content inside the child component's <ng-content> tag.
DIt ignores the parent content.
💡 Hint
Check the 'Action' and 'Resulting Render' columns at step 3 in the execution_table.
At which step does the browser finally render the combined content?
AStep 1
BStep 4
CStep 2
DStep 3
💡 Hint
Look for the step where 'Browser renders final DOM' is mentioned in the execution_table.
If the child template had no <ng-content> tag, what would happen to the parent content?
AIt would be ignored and not rendered inside the child.
BIt would still appear inside the child component.
CIt would cause an error.
DIt would be rendered outside the child component.
💡 Hint
Recall that is the placeholder for parent content projection as shown in the concept_flow.
Concept Snapshot
ng-content lets Angular insert parent HTML into child templates.
Parent writes content inside child tags.
Child uses <ng-content> as a placeholder.
Angular replaces <ng-content> with parent content.
Multiple slots possible with selectors.
No <ng-content> means no content projection.
Full Transcript
This visual trace shows how Angular's ng-content works for slot-based composition. The parent component writes HTML inside the child component's tag. The child component template contains an <ng-content> tag, which acts as a placeholder. Angular replaces this placeholder with the parent's content during rendering. The execution table shows step-by-step how the parent content is projected into the child template and finally rendered in the browser. Variables track the content and template changes. Key moments clarify why content appears inside the child and what happens without ng-content. The quiz tests understanding of these steps. This helps beginners see how Angular slots work visually.

Practice

(1/5)
1. What is the main purpose of ng-content in Angular components?
easy
A. To define component styles
B. To insert external content inside a component's template
C. To handle component events
D. To create services for dependency injection

Solution

  1. Step 1: Understand ng-content role

    ng-content is used to project or insert external HTML content into a component's template.
  2. Step 2: Differentiate from other features

    It does not define styles, handle events, or create services; those are unrelated to content projection.
  3. Final Answer:

    To insert external content inside a component's template -> Option B
  4. Quick Check:

    Content projection = Insert external content [OK]
Hint: Remember: ng-content projects external HTML inside components [OK]
Common Mistakes:
  • Confusing ng-content with event handling
  • Thinking ng-content styles components
  • Mixing ng-content with service creation
2. Which syntax correctly creates a named slot using ng-content in Angular?
easy
A.
B.
C.
D.

Solution

  1. Step 1: Recall correct attribute for named slots

    Angular uses the select attribute on ng-content to create named slots by CSS selectors.
  2. Step 2: Check each option

    Only uses select with a CSS class selector, which is correct syntax.
  3. Final Answer:

    <ng-content select=".header"></ng-content> -> Option A
  4. Quick Check:

    Named slot uses select attribute [OK]
Hint: Use select="CSS-selector" for named ng-content slots [OK]
Common Mistakes:
  • Using name or slot attributes which Angular does not support
  • Confusing with Web Components slot attribute
  • Using id attribute instead of select
3. Given this component template:
<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>?
medium
A.
B.

Welcome!

Hello

C.

Hello

Welcome!

D.

Hello

Welcome!

Solution

  1. Step 1: Match content with selectors

    The ng-content select=".title" inserts elements with class title, and select=".body" inserts elements with class body.
  2. Step 2: Check order of insertion

    The template places .title content first, then .body content, so the output matches that order.
  3. Final Answer:

    <div><h1 class="title">Hello</h1><p class="body">Welcome!</p></div> -> Option D
  4. Quick Check:

    Content order follows ng-content slots [OK]
Hint: Content projects in order of ng-content slots matching selectors [OK]
Common Mistakes:
  • Assuming order depends on usage, not template
  • Ignoring CSS class selectors in select
  • Expecting content without classes to appear
4. What is wrong with this component template?
<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>
medium
A. The usage classes do not match the template selectors
B. The select attribute cannot use class selectors
C. The selector 'header' misses the dot for class, so no content projects there
D. ng-content must have a closing tag with content inside

Solution

  1. Step 1: Analyze the select attribute usage

    The first ng-content uses select="header", which looks for elements named <header>, not class header.
  2. Step 2: Compare with usage classes

    The usage provides <div class="header">, which does not match the selector header (element selector). It should be .header to match the class.
  3. Final Answer:

    The selector 'header' misses the dot for class, so no content projects there -> Option C
  4. Quick Check:

    Class selectors need dot prefix in select [OK]
Hint: Class selectors in select need a dot prefix (e.g., .header) [OK]
Common Mistakes:
  • Using element name instead of class selector
  • Thinking select cannot use class selectors
  • Forgetting ng-content can be self-closing
5. You want to create a reusable card component with three slots: header, content, and footer. Which template correctly uses ng-content to support this slot-based composition?
hard
A.
B.
C.
D.

Solution

  1. Step 1: Identify correct attribute for named slots

    Angular uses select with CSS selectors to define named slots in ng-content.
  2. Step 2: Check each option's syntax

    uses select with class selectors, matching Angular's pattern. Options B, C, and D use invalid selectors or attributes (name, slot) not supported by Angular.
  3. Step 3: Confirm semantic HTML usage

    wraps slots in semantic elements (header, section, footer), improving accessibility and structure.
  4. Final Answer:

    Option A's template correctly uses ng-content with select and semantic tags -> Option A
  5. Quick Check:

    Use select with CSS classes for slots + semantic tags [OK]
Hint: Use select='.class' for slots and wrap with semantic tags [OK]
Common Mistakes:
  • Using name or slot attributes instead of select
  • Not using CSS selectors in select
  • Ignoring semantic HTML for accessibility
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard