Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Templates and Directives  ›  Lesson

Structural Directives

Structural Directives 12 minStructural directives such as *ngIf and *ngFor add and remove chunks of the page, and the star is shorthand for wrapping the element in an ng-template that the directive stamps out.

Structural Directives

Open almost any Angular project written before 2024 and you will see stars in the templates: *ngIf, *ngFor. Greenhouse will use the newer built-in blocks, but you need to read the older style fluently, and understanding the star explains what every "show this, repeat that" feature really does.

The rule

A star means "this element is a template the directive stamps out".

A structural directive changes the structure of the page: it adds or removes elements rather than changing how one looks.

<p *ngIf="plants.length === 0">No plants yet.</p>

<li *ngFor="let p of plants; let i = index">
  {{ i + 1 }}. {{ p.name }}
</li>

*ngIf renders the paragraph only while the condition is true. *ngFor renders one <li> per plant. Both come from @angular/common and must be imported (NgIf, NgFor, or the whole CommonModule).

The star is shorthand. Angular rewrites the element into an <ng-template>, a piece of markup that is not shown on its own, and hands it to the directive:

<ng-template [ngIf]="plants.length === 0">
  <p>No plants yet.</p>
</ng-template>

The directive then decides how many copies to stamp into the page: zero or one for ngIf, one per item for ngFor.

Two rules follow:

  • One star per element. Each star wants to wrap the element, and two wrappers on one element are ambiguous. Use <ng-container>, an invisible grouping tag, to carry the second one.
  • An else needs a named template: *ngIf="plants.length; else empty" with <ng-template #empty>...</ng-template>.

Modern Angular replaces these with the built-in @if, @for and @switch blocks, which need no imports and read more clearly. The star directives are marked deprecated in recent versions, but they still work, and you will meet them for years.

The shortcut

When you see a star, unfold it: picture the element wrapped in <ng-template>, and the directive deciding how many copies to print. Every puzzle about stars becomes a puzzle about templates.

Reading an old plant listStep 1 of 4
  1. Spot the star

    <li *ngFor="let p of plants"> is a template stamped once per plant.

Drills

Answer fast — trust the rule.

quick drill

What does the star in *ngIf stand for?

quick drill

How many structural directives may sit on one element?

quick drill

What does <ng-container> add to the rendered page?

quick drill

Which statement about *ngIf and @if is true?

Remember

  • Structural directives add and remove elements; the star unfolds into an ng-template.
  • One star per element; group with ng-container.
  • New code uses @if/@for/@switch, but read *ngIf/*ngFor fluently.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainUnfold the star

An older Greenhouse file contains <li *ngFor="let p of plants" *ngIf="p.indoor">{{ p.name }}</li>. Explain why Angular rejects it, what the star means, and how you would fix it both in the older directive style and with the built-in control flow.