Skip to content

prefer-control-flow

Reports a *ngIf or *ngFor attribute and a [ngSwitch] binding: the directives that the @if, @for and @switch blocks of Angular 17 replace. The *ngSwitchCase and *ngSwitchDefault inside a switch are not reported again.

The built-in blocks are part of the template syntax, so they need no import of CommonModule or of the directive, type-check their conditions more strictly and run without the directive’s runtime code. @for also requires a track expression, which *ngFor leaves to an optional trackBy.

To migrate, run ng generate @angular/core:control-flow.

Reported
...
<dialog><input autofocus /></dialog>
...
<p *ngIf="a">x</p>
<p *ngFor="let x of xs">x</p>
<div [ngSwitch]="a"><i *ngSwitchCase="1"></i></div>
...
<th scope="row"></th>
...
angular:
lint:
rules:
prefer-control-flow: warn # off (default), warn, error
html:
lint:
rules:
prefer-control-flow: warn # off (default), warn, error
svelte:
lint:
rules:
prefer-control-flow: warn # off (default), warn, error
vue:
lint:
rules:
prefer-control-flow: warn # off (default), warn, error

Enabled by the presets: html/angular-eslint-recommended.

Suppress one line with sudsy-disable-next-line prefer-control-flow in a comment of the file’s language.

The same check in other tools; Sudsy reports what each upstream rule reports.

Tool Rule Match
angular-eslint prefer-control-flow full