← Back to Angular, page 6

angular-0106 · Easy fresher

What is the difference between ng-container, ng-template and ng-content, and when do you use each?

ng-container is a grouping element that adds nothing to the DOM, ng-template defines a template that is not rendered until something instantiates it, and ng-content is a slot where content from the parent is projected. They solve different problems: grouping, deferred rendering and composition.

Full answer

These three look similar but have different jobs.

ng-container is an invisible wrapper. It lets you apply a structural directive or a binding to several elements without adding an extra div to the page. Typical uses: <ng-container *ngTemplateOutlet="...">, or grouping with a structural directive. Since the new control flow (@if, @for) does not need a host element, you need ng-container less often than before, but it is still useful with ngTemplateOutlet, and to apply a directive to a group.

ng-template defines a template that Angular does not render by itself. It becomes visible only when something creates it: a structural directive (*ngIf is sugar for <ng-template [ngIf]>), ngTemplateOutlet, ViewContainerRef.createEmbeddedView, or a component such as a tab or dialog that receives a TemplateRef. It can take parameters with let- variables: <ng-template let-user="user">. Use it to pass a piece of UI as data, for example to a table that lets you customise a cell.

ng-content is a slot in a component's own template. The parent writes content between the tags (<app-card>Hello</app-card>), and the card shows it where <ng-content /> appears. You can have several slots with select: <ng-content select="[title]" />. The projected content is created by the parent, so its bindings and styles belong to the parent.

Choosing:

  • Need to group without extra markup: ng-container.
  • Need to define reusable UI that is rendered later or many times: ng-template.
  • Need the user of your component to supply markup: ng-content (static) or ng-template passed in (dynamic, with context).

Remember that ng-content projects content that always exists, even if hidden, while content in an ng-template is created only when instantiated.

typescript
import { NgTemplateOutlet } from '@angular/common';
import { Component } from '@angular/core';

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

@Component({
  selector: 'app-demo',
  imports: [CardComponent, NgTemplateOutlet],
  template: `
    <app-card>
      <h2 title>Results</h2>
      <p>Everyone passed.</p>
    </app-card>

    <ng-template #row let-name let-marks="marks">
      <li>{{ name }}: {{ marks }}</li>
    </ng-template>

    <ul>
      <ng-container *ngTemplateOutlet="row; context: { $implicit: 'Asha', marks: 82 }" />
      <ng-container *ngTemplateOutlet="row; context: { $implicit: 'Ravi', marks: 91 }" />
    </ul>
  `,
})
export class DemoComponent {}

Angular 20.3, strict template check passes. A Karma test checks the projected title and body, and that the template outlet renders two rows with different context.

Key points

  • `ng-container` groups with no DOM element.
  • `ng-template` is not rendered until instantiated. It can take context variables.
  • `ng-content` is a slot for projected content, with optional `select`.
  • Use templates when the child must render the content several times or with data.

Common mistakes

  • Adding extra `div` wrappers when `ng-container` would do.
  • Expecting content inside an `ng-template` to exist before it is instantiated.
  • Using `ng-content` when each row needs data from the child, where a template with context is needed.

Follow-ups

  • How is `*ngIf` related to `ng-template`?
  • How do you project content into several slots?
  • What does `ngTemplateOutletContext` do?

Tips

  • Give one-line jobs: group, define, project.
  • Mention that `@if` and `@for` reduced the need for `ng-container`.
  • Show a template with context.

Evaluation rubric

  • Distinguishes the three correctly.
  • Gives uses for each.
  • Understands creation timing and context.