Angular Architecture: Structural Directives

Master conditional rendering (*ngIf) and dynamic lists (*ngFor). Learn how to architect the DOM structure of your Angular applications effectively.

Lesson ProgressStep 1 of 8
Component
*ngIf
*ngFor Item
0 EXP

Welcome! Let's explore how Angular manipulates the DOM. Structural directives are the architects of your view.

Conditional Rendering with *ngIf

The `*ngIf` directive is used to conditionally include or exclude a template based on the value of an expression.

<div *ngIf="condition">I exist only if condition is true</div>

Unlike CSS `display: none` which hides an element visually, `*ngIf` removes the element from the DOM entirely. This preserves resources.

System Check

What happens to an element when *ngIf evaluates to false?

Advanced Template Simulations

0 EXP

Apply your knowledge in these advanced simulations. All our content is free!


Achievements

🏗️
Directive Architect

Construct a template using both conditional and list directives.

🔁
Loop Legend

Correctly order the structural elements of an *ngFor loop.

🩺
Syntax Surgeon

Prove your mastery of the asterisk (*) microsyntax.

Mission: Build a Conditional List

Create a `<ul>`. Inside, use `*ngFor` to loop through items. Also, use `*ngIf` on the container or the items to simulate a condition. Our AI assistant will validate your directive syntax.

Compiler Feedback:

> Awaiting template input...

Challenge: Construct the Loop

Arrange the code blocks to create a valid unordered list iterating over users.

<li *ngFor='let user of users'>
<ul>
{{ user.name }}
</li>
</ul>

Challenge: Microsyntax Mastery

Fill in the missing directives to complete the template.

<div="isLoggedIn">
<div="let item of items;: item.id">
{{ "item.name" }}
</div>
</div>

Consult the Architect (AI)

Community Dev-Net

Peer Project Review

Submit your "Dynamic Dashboard" project using structural directives for code review.

Controlling the DOM: The Power of Structural Directives

Angular applications are dynamic. Elements appear, disappear, and duplicate based on user interaction and data. The tools that allow you to manipulate the DOM layout by adding, removing, and manipulating elements are called **Structural Directives**.

The Asterisk (*): Syntactic Sugar

You've likely noticed that directives like `*ngIf` and `*ngFor` are prefixed with an asterisk. This isn't just decoration. It's shorthand that tells Angular to wrap the host element in an <ng-template> .

What You Write

<div *ngIf="hero" class="name">
  {{hero.name}}
</div>

What Angular Compiles

<ng-template [ngIf]="hero">
  <div class="name">
    {{hero.name}}
  </div>
</ng-template>

Performance: `*ngIf` vs `[hidden]`

New developers often confuse `*ngIf` with the `[hidden]` attribute. While they appear visually similar, they function very differently:

  • *ngIf: Physically removes the element from the DOM. If the condition is false, the component is destroyed, freeing up memory and resources.
  • [hidden]: Merely sets `display: none` via CSS. The element remains in the DOM and the component stays alive.

Best Practice: Use `*ngIf` by default, especially for large trees of components. Use `[hidden]` only if the toggle cost is high and you need to keep the state of the component alive while it is invisible.

Optimizing Lists with `trackBy`

When using `*ngFor` to iterate over large lists fetched from an API, Angular has to re-render the DOM whenever the list reference changes. This can be expensive.

By using a `trackBy` function, you tell Angular how to identify unique items (usually by ID). This allows Angular to only update the DOM elements that have actually changed, rather than destroying and recreating the entire list.

Key Takeaway: Structural directives are powerful tools for managing your view. Understand the cost of DOM manipulation and use tools like `trackBy` and `ngSwitch` to keep your application performant.

Angular Structural Directives Glossary

Structural Directive
A directive that handles how the component or element renders in the DOM. They modify the DOM layout by adding, removing, or manipulating elements.
*ngIf
A structural directive that conditionally includes a template based on the value of an expression coerced to Boolean.
*ngFor
A structural directive that renders a template for each item in a collection.
[ngSwitch]
A directive that switches among a number of alternative views. It acts as a container for `*ngSwitchCase` and `*ngSwitchDefault`.
<ng-template>
An Angular element for rendering HTML. It is never displayed directly. Angular wraps structural directive content in this element.
<ng-container>
A logical container that doesn't render in the DOM. Useful for applying structural directives without adding extra HTML elements (like `div`s) to the DOM.
trackBy
A function used with `*ngFor` to define how to track changes for items in an iterable, improving performance by reducing DOM operations.
Microsyntax
The specific syntax used inside structural directives (e.g., `let item of items; index as i`).

About the Author

Author's Avatar

TodoTutorial Team

Passionate developers and educators making programming accessible to everyone.

This article was written and reviewed by our team of Angular experts, ensuring alignment with the latest Angular version best practices.

Verification and Updates

Last reviewed: October 2025.

We strive to keep our content accurate. This tutorial reflects standards for Angular 17+.

External Resources

Found an error or have a suggestion? Contact us!