← Back to Angular, page 2

angular-0030 · Medium mid

How does content projection work with ng-content, and what are ng-container and ng-template used for?

ng-content lets a parent place its own markup inside a child component, optionally into named slots with a select attribute. ng-container groups elements without adding a DOM node. ng-template defines markup that is not rendered until something, such as ngTemplateOutlet, instantiates it, often with a context object.

Full answer

Content projection is how a reusable component accepts markup from its user, like a card whose header and body are written by whoever uses it.

<ng-content /> in the child's template marks where the projected content goes. With several slots, add select: <ng-content select="[card-title]" /> receives only elements that have the card-title attribute. The selector can be a tag, a class, or an attribute. Content that matches no slot goes to the plain <ng-content />, if there is one. Projected content is created by the parent, so its bindings use the parent's data, and it exists even if the child never shows it.

<ng-container> is an invisible grouping element. It exists in the template but renders no DOM node. Use it to apply a control-flow block or a directive to several siblings without an extra div.

<ng-template> holds markup that is not rendered by default. Something must instantiate it. ngTemplateOutlet does this: <ng-container [ngTemplateOutlet]="tpl" [ngTemplateOutletContext]="{ $implicit: item, index: i }" />. The template reads the context with let-item and let-i="index".

This is the way to let a parent customise how each row is drawn: the list component loops over data and the parent supplies the row template, found by the child through contentChild.

Legacy note: older code uses *ngIf="cond; else other" with ng-template. With the new @if/@else this is not needed, but ng-template is still very much current for outlets and custom structural directives.

typescript
import { NgTemplateOutlet } from '@angular/common';
import { Component, TemplateRef, contentChild, input } from '@angular/core';

@Component({
  selector: 'app-card',
  template: `
    <header><ng-content select="[card-title]" /></header>
    <section><ng-content /></section>
    <footer><ng-content select="[card-actions]" /></footer>
  `,
})
export class CardComponent {}

interface RowContext {
  $implicit: string;
  index: number;
}

@Component({
  selector: 'app-item-list',
  imports: [NgTemplateOutlet],
  template: `
    @for (item of items(); track item; let i = $index) {
      <ng-container
        [ngTemplateOutlet]="row()"
        [ngTemplateOutletContext]="{ $implicit: item, index: i }"
      />
    }
  `,
})
export class ItemListComponent {
  items = input.required<string[]>();
  row = contentChild.required<TemplateRef<RowContext>>('row');
}

@Component({
  selector: 'app-demo-page',
  imports: [CardComponent, ItemListComponent],
  template: `
    <app-card>
      <h2 card-title>Toppers</h2>
      <app-item-list [items]="names">
        <ng-template #row let-name let-i="index">
          <p>{{ i + 1 }}. {{ name }}</p>
        </ng-template>
      </app-item-list>
      <button card-actions>Print</button>
    </app-card>
  `,
})
export class DemoPageComponent {
  names = ['Asha', 'Imran', 'Meera'];
}

Angular 20.3, strict template checks pass. The parent supplies one `ng-template` named `row`, which the list repeats for each item.

Key points

  • `<ng-content select="...">` creates named slots for projected content.
  • `ng-container` groups without a DOM node. `ng-template` is not rendered until instantiated.
  • `ngTemplateOutlet` plus a context object lets a parent control how items render.
  • `contentChild` lets the child find the template the parent passed in.

Common mistakes

  • Putting `<ng-content>` inside an `@if` and assuming the projected component is not created when the condition is false. The parent still creates it. Only its display is skipped.
  • Using `ng-template` and wondering why nothing shows. It needs an outlet or a directive.
  • Wrapping everything in extra `div` elements instead of `ng-container`, which breaks CSS layouts.

Follow-ups

  • What is the difference between `contentChild` and `viewChild`?
  • Who owns the change detection of projected content?
  • How can a component project the same content in two places?

Tips

  • Use a card with a title slot. Everyone understands it quickly.
  • Know the template context terms `$implicit` and `let-name="key"`.
  • Mention that `ngTemplateOutlet` is still the right tool even after control flow replaced `*ngIf`.

Evaluation rubric

  • Uses ng-content with select.
  • Explains ng-container and ng-template roles.
  • Uses ngTemplateOutlet with context.