← Back to list

Understanding the Difference Between *ngFor and @for in Angular

With the release of Angular 17, developers have gained access to a new syntax, @for, which introduces a fresh approach to iterating over…

Priyabrata Saha · 2024-12-07 07:29 · 137 claps · 2.3 min read paywalled
#angular #angular-17-new-feature #ngfor-in-angular #ngfor
Open on Medium ↗
Wiki topics: LNG · Linguistics & Language 🌐 · Web Development

Understanding the Difference Between *ngFor and @for in Angular

With the release of Angular 17, developers have gained access to a new syntax, @for, which introduces a fresh approach to iterating over collections in templates. This addition complements the long-standing *ngFor directive, providing more options for different use cases. Understanding their differences is crucial for leveraging the right tool based on your project requirements.

1. What is *ngFor?

*ngFor is a structural directive used to iterate over a collection and dynamically render its elements in an Angular template. It has been a core feature since Angular's early versions.

Key Features of *ngFor:

  • Declarative Syntax: Ideal for straightforward template rendering.
  • TrackBy Functionality: Enhances performance by allowing Angular to identify items uniquely and avoid unnecessary re-renders.
  • Template Binding: Supports accessing indexes, even/odd states, and first/last positions within loops.

Example:

<ul>
  <li *ngFor="let item of items; let i = index; trackBy: trackByFn">
    {{ i }}: {{ item.name }}
  </li>
</ul>

Best Use Case:

*ngFor is perfect when the main goal is to render a collection directly into the DOM with minimal preprocessing.

2. What is @for?

The new @for syntax in Angular 17 simplifies looping within templates, combining modern control-flow capabilities with built-in optimizations. Unlike *ngFor, @for is more intuitive and flexible, handling data processing directly in the component logic or inline within templates.

Key Features of @for:

  • Mandatory Tracking: Requires a track expression to optimize performance.
  • Supports @empty Block: Allows rendering default content if a collection is empty.
  • Handles Iterables: Works seamlessly with arrays, strings, or iterable objects like Map or Set.

Example:

<ul>
  @for (item of items; track item.id) {
    <li>{{ item.name }}</li>
  }
  @empty {
    <li>No items found</li>
  }
</ul>

Best Use Case:

@for is better suited for cases requiring:

  • Complex data preprocessing before rendering.
  • Efficient handling of empty collections.
  • Seamless integration with modern iterable objects.

3. Key Differences

4. Performance Considerations

*ngFor:

While powerful, *ngFor can lead to unnecessary DOM re-renders if not optimized using trackBy.

@for:

Designed with performance in mind, @for enforces the use of a track function or expression. This ensures Angular efficiently tracks and re-renders only the affected items, making it a safer option for large or frequently updated collections.

5. When to Use Each

Use *ngFor When:

  • Rendering collections with minimal logic.
  • You already have trackBy implemented for performance.
  • Your application uses Angular versions prior to 17.

Use @for When:

  • Migrating to Angular 17 or later.
  • Processing complex data transformations in templates.
  • Building dynamic UIs that require default states for empty collections.

Conclusion

The introduction of @for in Angular 17 marks a significant step forward in control-flow capabilities. While *ngFor remains a reliable option for simple iterations, @for offers better performance, readability, and modern features like native support for empty states. By understanding their differences, developers can make informed decisions to optimize their Angular applications.

For further insights, explore resources like Angular’s official documentation and guides on modern control-flow syntax​ ByteGoblin, Angular University, Tevpro.com, Angular.

*If you liked the article please buy me a coffee 😃 by clicking here*


메타데이터
post_id
b9b4b41a703a
slug
understanding-the-difference-between-ngfor-and-for-in-angular-b9b4b41a703a
url
https://medium.com/@stream2085/understanding-the-difference-between-ngfor-and-for-in-angular-b9b4b41a703a
canonical_url
https://medium.com/@stream2085/understanding-the-difference-between-ngfor-and-for-in-angular-b9b4b41a703a
author_url
https://medium.com/@stream2085
status
ok
fetched_at
2026-07-21 19:35:56