Thuta Learning
Angular
IntermediateWeb DevelopmentIntermediate

Structural: *ngIf

AngularLesson 1220 min

What you'll walk away with

  • Explain the core concept and Angular data flow of Structural: *ngIf
  • Run the example, predict its state and UI, and test edge and error cases
  • Apply a typed safe pattern in a real project and recognize common pitfalls

Explain it simply

Structural: *ngIf, in plain language

*ngIf directive is used to add or remove an element from the DOM based on a condition.

Problem → Why → Solution

The problem — why this concept matters

Using only !items.length cannot distinguish not-yet-loaded data from a genuinely empty result.

A common mistake

One happy-path result does not prove the feature works; empty, loading, error, and cleanup behavior need explicit tests.

Core concept — understand how it works

Conditional rendering can create and destroy a view rather than merely hiding an element. You may encounter both legacy *ngIf microsyntax and modern built-in @if control flow; aliases and else branches reduce repeated nullable access. Model loading, empty, error, and success states explicitly instead of overloading one Boolean.

Mental model
  1. input and state
  2. component and template
  3. rendered Angular UI

Explain the core concept and Angular data flow of Structural: *ngIf

Core syntax or pattern

typescript
<div *ngIf="isLoggedIn">
  <p>Welcome back, user!</p>
</div>
<div *ngIf="!isLoggedIn">
  <p>Please log in.</p>
</div>

// component.ts
export class MyComponent {
  isLoggedIn = true;
}

Conditional rendering can create and destroy a view rather than merely hiding an element. You may encounter both legacy *ngIf microsyntax and modern built-in @if control flow; aliases and else branches reduce repeated nullable access. Model loading, empty, error, and success states explicitly instead of overloading one Boolean.

Read the code step by step

  1. input and state<div *ngIf="isLoggedIn">

    *ngIf directive is used to add or remove an element from the DOM based on a condition.

  2. component and template</div>

    Conditional rendering can create and destroy a view rather than merely hiding an element. You may encounter both legacy *ngIf microsyntax and modern built-in @if control flow; aliases and else branches reduce repeated nullable access. Model loading, empty, error, and success states explicitly instead of overloading one Boolean.

  3. rendered Angular UI}

    Build an API-result UI with loading, error, empty, and success branches and render every branch with mock state.

Execution flow
  1. input and state
  2. component and template
  3. rendered Angular UI

Success

Explain the core concept and Angular data flow of Structural: *ngIf

Failure

Using only !items.length cannot distinguish not-yet-loaded data from a genuinely empty result.

*ngIf directive is used to add or remove an element from the DOM based on a condition.

typescript
<div *ngIf="isLoggedIn">
  <p>Welcome back, user!</p>
</div>
<div *ngIf="!isLoggedIn">
  <p>Please log in.</p>
</div>

// component.ts
export class MyComponent {
  isLoggedIn = true;
}
You should see
(Since isLoggedIn is true, the text 'Welcome back, user!' will show up)

Real-world usage

How to apply it in a real project

Build an API-result UI with loading, error, empty, and success branches and render every branch with mock state.

Mini challenge

Build it yourself

Rebuild the Structural: *ngIf example without copying it. Add two normal states, one edge case, and one failure state, and write the expected UI and state transition before running it.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
typescript
<div *ngIf="isLoggedIn">
  <p>Welcome back, user!</p>
</div>
<div *ngIf="!isLoggedIn">
  <p>Please log in.</p>
</div>

// component.ts
export class MyComponent {
  isLoggedIn = true;
}

Expected: Explain the core concept and Angular data flow of Structural: *ngIf

Show hint

Build an API-result UI with loading, error, empty, and success branches and render every branch with mock state.

Quick quiz 1 / 3

What best describes the core idea of Structural: *ngIf?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • input and state → component and template → rendered Angular UI
  • Conditional rendering can create and destroy a view rather than merely hiding an element. You may encounter both legacy *ngIf microsyntax and modern built-in @if control flow; aliases and else branches reduce repeated nullable access. Model loading, empty, error, and success states explicitly instead of overloading one Boolean.
  • Using only !items.length cannot distinguish not-yet-loaded data from a genuinely empty result.
  • Build an API-result UI with loading, error, empty, and success branches and render every branch with mock state.

Angular — Templates — Angular

Easy traps

  • Using only !items.length cannot distinguish not-yet-loaded data from a genuinely empty result.
  • One happy-path result does not prove the feature works; empty, loading, error, and cleanup behavior need explicit tests.

Hands-on Exercise

Rebuild the Structural: *ngIf example without copying it. Add two normal states, one edge case, and one failure state, and write the expected UI and state transition before running it.

You'll know it worked when: (Since isLoggedIn is true, the text 'Welcome back, user!' will show up)

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Structural: *ngIf | Thuta Learning