Control Flow

Loading concept...

๐ŸŽฎ Angular Control Flow: The Traffic Controller of Your App

Imagine youโ€™re a traffic controller at a busy intersection. Cars (data) are coming from everywhere, and YOU decide:

  • Which cars go through (show this content)
  • Which cars wait (hide this content)
  • Which lane each car takes (show different content based on conditions)

Thatโ€™s exactly what Angular Control Flow does for your templates!


๐Ÿšฆ The Big Picture

Angular 17+ introduced a brand new way to control what shows up on your screen. Think of it like having magic traffic signs that appear and disappear based on rules you set.

graph TD A[Your Data] --> B{Control Flow} B -->|if| C[Show This?] B -->|for| D[Repeat This?] B -->|switch| E[Which One?] C --> F[Display Content] D --> F E --> F

๐ŸŽฏ The @if Block - The Bouncer

What Is It?

Think of @if like a bouncer at a club. The bouncer checks if you meet the requirements. If yes, you get in. If no, you stay outside!

Simple Example

Scenario: Show a welcome message only if the user is logged in.

@if (isLoggedIn) {
  <p>Welcome back, friend! ๐ŸŽ‰</p>
}

What happens?

  • If isLoggedIn is true โ†’ Message appears!
  • If isLoggedIn is false โ†’ Nothing shows

The @else Friend

Sometimes you want to show something ELSE when the condition is false. Like a sign that says โ€œSorry, weโ€™re closed!โ€

@if (isLoggedIn) {
  <p>Welcome back! ๐ŸŽ‰</p>
} @else {
  <p>Please log in first ๐Ÿ”</p>
}

The @else if Chain

What if you have MULTIPLE conditions? Like checking age groups:

@if (age < 13) {
  <p>Kid's Menu ๐Ÿ•</p>
} @else if (age < 20) {
  <p>Teen Menu ๐Ÿ”</p>
} @else {
  <p>Adult Menu ๐Ÿฅ—</p>
}

Itโ€™s like a waterfall:

  1. Check first condition
  2. If false, check next
  3. Keep going until one is true
  4. If none are true, use @else

๐Ÿ”„ The @for Block - The Copy Machine

What Is It?

Imagine you have a magic copy machine. You put ONE template in, and it creates copies for every item in your list!

Simple Example

Scenario: You have a list of fruits. Show each one.

// In your component
fruits = ['Apple', 'Banana', 'Cherry'];
@for (fruit of fruits; track fruit) {
  <p>I love {{ fruit }}! ๐ŸŽ</p>
}

Output on screen:

I love Apple! ๐ŸŽ
I love Banana! ๐ŸŽ
I love Cherry! ๐ŸŽ

The Magic track Word

Super Important! You MUST use track with @for.

Think of track like name tags at a party. Angular uses these to remember who is who. Without name tags, if someone leaves, Angular might get confused about whoโ€™s still there!

<!-- Using the item itself as ID -->
@for (fruit of fruits; track fruit) {
  <p>{{ fruit }}</p>
}

<!-- Using an index number -->
@for (fruit of fruits; track $index) {
  <p>{{ $index }}: {{ fruit }}</p>
}

<!-- Using a unique ID (best practice!) -->
@for (user of users; track user.id) {
  <p>{{ user.name }}</p>
}

Built-in Variables (Free Gifts! ๐ŸŽ)

When you use @for, Angular gives you FREE helper variables:

Variable What It Means Example
$index Position number (starts at 0) 0, 1, 2โ€ฆ
$first Is this the first item? true/false
$last Is this the last item? true/false
$even Is the position even? true/false
$odd Is the position odd? true/false
$count Total number of items 3

Fun Example:

@for (friend of friends; track friend.id) {
  <div>
    @if ($first) {
      <span>๐Ÿ‘‘</span>
    }
    {{ friend.name }}
    @if ($last) {
      <span>๐Ÿ</span>
    }
  </div>
}

The @empty Helper

What if your list is EMPTY? Show a friendly message!

@for (item of cart; track item.id) {
  <p>{{ item.name }}</p>
} @empty {
  <p>Your cart is empty! ๐Ÿ›’</p>
}

๐Ÿ”€ The @switch Block - The Sorting Hat

What Is It?

Remember the Sorting Hat from Harry Potter? It looks at a student and says โ€œYou belong in Gryffindor!โ€ or โ€œYou belong in Slytherin!โ€

@switch does the same thing - it looks at a value and picks the matching case!

Simple Example

Scenario: Show different greetings based on time of day.

// In your component
timeOfDay = 'morning';
@switch (timeOfDay) {
  @case ('morning') {
    <p>Good morning! โ˜€๏ธ</p>
  }
  @case ('afternoon') {
    <p>Good afternoon! ๐ŸŒค๏ธ</p>
  }
  @case ('evening') {
    <p>Good evening! ๐ŸŒ™</p>
  }
  @default {
    <p>Hello there! ๐Ÿ‘‹</p>
  }
}

When to Use @switch vs @if

Use @switch Whenโ€ฆ Use @if Whenโ€ฆ
Checking ONE value against MANY options Checking TRUE/FALSE
Value is exact match (===) Complex conditions
3+ options 1-2 options

Good @switch:

@switch (status) {
  @case ('pending') { <span>โณ</span> }
  @case ('approved') { <span>โœ…</span> }
  @case ('rejected') { <span>โŒ</span> }
}

Good @if:

@if (score > 90) {
  <p>Excellent!</p>
}

๐Ÿท๏ธ Track Expression - The Name Tag System

Why Track Matters

Imagine you have 100 students in a classroom. The teacher needs to know WHO is WHO. If a new student joins or one leaves, the teacher shouldnโ€™t have to re-learn everyoneโ€™s name!

track gives each item a unique ID so Angular can:

  • โœ… Update ONLY what changed
  • โœ… Keep the page fast
  • โœ… Avoid flickering

Track Options

Option 1: Track by the item itself (simple data)

@for (color of colors; track color) {
  <span>{{ color }}</span>
}

Best for: simple strings or numbers

Option 2: Track by index (position number)

@for (item of items; track $index) {
  <span>{{ item }}</span>
}

Best for: static lists that donโ€™t change

Option 3: Track by unique ID (BEST PRACTICE! ๐Ÿ†)

@for (user of users; track user.id) {
  <div>{{ user.name }}</div>
}

Best for: objects with unique IDs

Option 4: Track by function

@for (product of products;
      track trackByProductId(product)) {
  <div>{{ product.name }}</div>
}
trackByProductId(product: Product) {
  return product.id;
}

Best for: complex tracking logic

Common Mistakes โŒ

<!-- โŒ WRONG: No track! -->
@for (item of items) {
  <p>{{ item }}</p>
}

<!-- โœ… CORRECT: Has track! -->
@for (item of items; track item.id) {
  <p>{{ item }}</p>
}

๐ŸŽจ Putting It All Together

Letโ€™s build a Task Manager using everything we learned!

<h1>My Tasks ๐Ÿ“‹</h1>

@if (tasks.length > 0) {
  @for (task of tasks; track task.id) {
    <div class="task">
      <!-- Position badge -->
      <span>{{ $index + 1 }}.</span>

      <!-- Task name -->
      <span>{{ task.name }}</span>

      <!-- Status with switch -->
      @switch (task.status) {
        @case ('pending') {
          <span>โณ Waiting</span>
        }
        @case ('doing') {
          <span>๐Ÿ”จ In Progress</span>
        }
        @case ('done') {
          <span>โœ… Complete</span>
        }
        @default {
          <span>โ“ Unknown</span>
        }
      }

      <!-- Special badges -->
      @if ($first) {
        <span>โญ First!</span>
      }
    </div>
  } @empty {
    <p>No tasks yet!</p>
  }
} @else {
  <p>Loading tasks... โณ</p>
}

๐Ÿง  Quick Memory Tips

Control Flow Think Of It Asโ€ฆ Use Whenโ€ฆ
@if Bouncer Show/hide based on TRUE/FALSE
@for Copy machine Repeat for each item in list
@switch Sorting hat Pick ONE from MANY exact matches
track Name tags Help Angular remember items

๐Ÿš€ Key Takeaways

  1. @if = Show content when condition is true
  2. @else = Fallback when @if is false
  3. @for = Repeat content for each list item
  4. track = REQUIRED with @for (like a name tag)
  5. @empty = Show when list has no items
  6. @switch = Pick from multiple exact-match options
  7. @default = Fallback when no @case matches

Youโ€™re now the Traffic Controller of your Angular app! ๐Ÿšฆ

Go forth and direct that data! ๐ŸŽฎ

Loading story...

No Story Available

This concept doesn't have a story yet.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Interactive Content

This concept doesn't have interactive content yet.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Cheatsheet Available

This concept doesn't have a cheatsheet yet.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Quiz Available

This concept doesn't have a quiz yet.