Bird
Raised Fist0
Angularframework~5 mins

ng-content for slot-based composition in Angular

Choose your learning style10 modes available
Introduction

ng-content lets you insert content from outside a component into specific places inside it. This helps build flexible and reusable components.

You want to create a card component that can show different content inside it.
You need a modal dialog where the title, body, and footer come from outside.
You want to build a reusable layout component that accepts different sections like header and footer.
You want to pass buttons or links into a toolbar component from outside.
You want to separate structure from content so components are easier to maintain.
Syntax
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.

Examples
Inserts all passed content exactly where this tag is placed.
Angular
<ng-content></ng-content>
Inserts content with class header in the first slot and content with class body in the second slot.
Angular
<ng-content select=".header"></ng-content>
<ng-content select=".body"></ng-content>
Inserts all <h1> elements in the first slot and all <p> elements in the second slot.
Angular
<ng-content select="h1"></ng-content>
<ng-content select="p"></ng-content>
Sample Program

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.

Angular
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 {}
OutputSuccess
Important Notes

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.

Summary

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

(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