🔁 Angular 20 Control Flow — Part 2: `@for` is the New `*ngFor`
vetriselvan Panneerselvam

vetriselvan Panneerselvam @vetriselvan_11

About: 👋 Hi, I'm Vetriselvan. A passionate front-end developer who loves turning ideas into clean, functional, and user-friendly interfaces. I enjoy writing code and sharing what I learn along the way.

Joined:
Jun 10, 2025

🔁 Angular 20 Control Flow — Part 2: `@for` is the New `*ngFor`

Publish Date: Jun 15
6 3

Hey again, Angular devs! 👋

In Part 1, we explored the new @if, @else if, and @else blocks in Angular 20’s control flow syntax.
Now it's time to tackle loops with the powerful and expressive @for block.

🔁 What is @for?

The @for block in Angular 20 allows you to render a block of content repeatedly for each item in a collection — essentially replacing the traditional *ngFor.

Syntax :

  @for (item of collection; track uniqueKey) {
     <li>{{ item}}</li>
   } @empty {
     <li>There are no items.</li>
   }
Enter fullscreen mode Exit fullscreen mode

📌 Note:

You can optionally include an @empty section immediately after the @for block content. The content of the @empty block displays when there are no items.

🧪 Example 1: Basic Looping with @for

Here's a simple example demonstrating the use of @if and @for to render a list of countries in the template. As shown in the code below, a country API is triggered, and the country$ observable provides the list of countries. We use the async pipe to subscribe to the observable, and the @for loop is used to display each country in the template.

🌐 Template:

<div class="country-list">
  @if (country$ | async; as countryList) {
    @for (country of countryList; track $index) {
      <div class="country">
        <span>{{ country.flag }}</span>
        <span>{{ country.name.common }}</span>
      </div>
    }
  }
</div>
Enter fullscreen mode Exit fullscreen mode

📘 Component (TypeScript):

country$ = this.http.get<any>(this.URL).pipe(
  map((res: any) => res)
);
Enter fullscreen mode Exit fullscreen mode

📌 Note:

  • @for is async-safe, allowing you to combine it neatly with @if and observables.
  • You must use track to optimize performance by helping Angular identify items uniquely.

❓ Wait… What's This track Keyword?

Great question!

track:

The track keyword provides a unique key for each item in the collection. This helps Angular perform efficient DOM updates when the collection changes (e.g., when items are added, removed, or reordered).

It's like trackBy in *ngFor, but much cleaner.

@for (item of items; track item.id) {
  <!-- unique key is item.id -->
}
Enter fullscreen mode Exit fullscreen mode

🧠 Implicit Variables in @for

Angular gives us a set of built-in variables inside a @for block:

Variable Meaning
$count Total number of items
$index Index of the current item (starts at 0)
$first true if this is the first item
$last true if this is the last item
$even true if the index is even
$odd true if the index is odd

🧪 Example 2: Using All Implicit Variables

<div class="country-list">
  @if (country$ | async; as countryList) {
    @for (
      country of countryList;
      track $index;
      let index = $index,
          even = $even,
          odd = $odd,
          last = $last,
          first = $first,
          count = $count
    ) {
      <div class="country">
        <div class="label">
          <span>{{ index }} : {{ country.flag }}</span>
          <span>{{ country.name.common }}</span>
        </div>
        <hr />
        <div class="variable">
          <span style="color: red">Odd: {{ odd }}</span>
          <span style="color: blue">Even: {{ even }}</span>
          <span style="color: aqua">Last: {{ last }}</span>
          <span style="color: orange">First: {{ first }}</span>
          <span style="color: green">Count: {{ count }}</span>
        </div>
      </div>
    }
  }
</div>
Enter fullscreen mode Exit fullscreen mode

Output

Image description

❗ Heads-Up

Angular's @for block does not support flow-modifying statements like break or continue — unlike traditional JavaScript loops.

✅ Summary for Part 2

  • Use @for to iterate over collections in Angular 20.
  • Use track for optimized rendering.
  • Leverage built-in loop metadata with $index, $count, etc.
  • Combine it with @if for reactive, observable-based logic.

👉 In Part 3, we’ll dive into @switch and how to manage multiple conditional templates elegantly. If you missed Part 1, go check it out!

✍️ Author: Vetriselvan

👨‍💻 Frontend Developer | Code Lover | Exploring Angular’s future

Comments 3 total

  • Info
    InfoJun 15, 2025

    If you've published on Dev.to, read this: your special Dev.to drop now live for Dev.to contributors in recognition of your efforts on Dev.to! Claim your rewards here (for verified Dev.to users only). – Dev.to Airdrop Desk

  • DevOps Fundamental
    DevOps FundamentalJun 28, 2025

    Such a valuable read—keep them coming!

Add comment