DEV Community

Cover image for 🔧🚫 Stop Using ngIf and ngFor: Enforce Angular's New Control Flow with ESLint ✅
jayasooriya-s
jayasooriya-s

Posted on

2 1

🔧🚫 Stop Using ngIf and ngFor: Enforce Angular's New Control Flow with ESLint ✅

Still typing *ngFor, *ngIf, and *ngSwitch out of habit? You’re not alone. After years of working with legacy Angular, my muscle memory made it hard to switch to the latest Angular control flow syntax. But with one simple trick, I finally made the shift—and it improved both my code and performance. In this post, I’ll show you how to ditch the old directives and get comfortable with Angular’s new control flow in no time

The Simple Trick? Let ESLint Do the Work for You
Instead of forcing myself to remember, I let ESLint catch me in the act.

Angular now supports ESLint rules that can warn or error when you use legacy structural directives like *ngFor, *ngIf, and *ngSwitch and instead suggest using the new control flow syntax @for, @if, @switch.

Before Eslint Rule:
code sinippet before eslint rule

After Eslint Rule:
After adding the rule code editor highligts the error
code sinippet after eslint rule

Solution: Add This ESLint Rule to Your Project
To make the switch easier, you can add the following rule to your eslint.config.js under the html rules section:

  {
    files: ["**/*.html"],
    extends: [
      ...angular.configs.templateRecommended,
      ...angular.configs.templateAccessibility,
    ],
    rules: {
      "@angular-eslint/template/prefer-control-flow": "error"
    },
  }

Enter fullscreen mode Exit fullscreen mode

"@angular-eslint/template/prefer-control-flow": "error"

Once you add this rule, run ng lint to catch any instances of the old directives:

 12:37  error  Use built-in control flow instead of directive ngIf  @angular-eslint/template/prefer-control-flow
  16:15  error  Use built-in control flow instead of directive ngIf  @angular-eslint/template/prefer-control-flow
Enter fullscreen mode Exit fullscreen mode

after error fixed

Benefits of Using the New Syntax
Switching to Angular’s new control flow syntax isn’t just about cleaner code—it’s also about performance improvements. The new directives are optimized to work faster, which can make a noticeable difference in larger applications.

Final Thoughts
Switching from *ngIf, *ngFor, and *ngSwitch to Angular’s new control flow syntax can be a tough habit to break—but with the right ESLint rules in place, the transition becomes effortless. You’ll write cleaner, more performant code without having to constantly remind yourself to use the new syntax.

Want to take it a step further? Combine this lint rule with a commit-stage linting setup to automatically prevent legacy control flow usage from making it into your codebase. In this related guide, I walk you through how to do exactly that:
👉 Maintain Code Quality Like a Big Tech Team Without Paying for Expensive Automation

By combining ESLint rules with pre-commit hooks, you ensure that your team consistently follows the latest Angular best practices—with zero manual effort.

🖼️ Banner generated using OpenAI Sora

Runner H image

An AI Agent That Handles Life, Not Just Work

From ordering flowers to booking your dinner — let Runner H turn your ideas into actions. No prompts, no hassle. Just outcomes.

Try Runner H

Top comments (1)

Collapse
 
nevodavid profile image
Nevo David

Honestly, changing old habits like this feels rough but I'm down with anything that keeps my code clean and quick.

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Take a moment to explore this thoughtful article, beloved by the supportive DEV Community. Coders of every background are invited to share and elevate our collective know-how.

A heartfelt "thank you" can brighten someone's day—leave your appreciation below!

On DEV, sharing knowledge smooths our journey and tightens our community bonds. Enjoyed this? A quick thank you to the author is hugely appreciated.

Okay