Angular’s @angular/common package provides essential directives for manipulating the DOM structure and styling elements in your templates.
Deprecation Notice: Structural directives (ngIf, ngFor, ngSwitch) are deprecated as of Angular v20 and will be removed in v22. Use the new control flow syntax instead.
<!-- Simple usage --><div *ngIf="condition">Content to render when condition is true.</div><!-- With else block --><div *ngIf="condition; else elseBlock">Content when true</div><ng-template #elseBlock>Content when false</ng-template><!-- With then and else --><div *ngIf="condition; then thenBlock else elseBlock"></div><ng-template #thenBlock>Content when true</ng-template><ng-template #elseBlock>Content when false</ng-template><!-- Store value locally --><div *ngIf="user$ | async as user"> Welcome, {{ user.name }}!</div>
@Directive({ selector: '[ngFor][ngForOf]'})export class NgForOf<T, U extends NgIterable<T> = NgIterable<T>> { @Input() ngForOf: (U & NgIterable<T>) | undefined | null; @Input() ngForTrackBy: TrackByFunction<T>; @Input() ngForTemplate: TemplateRef<NgForOfContext<T, U>>;}
<!-- Basic iteration --><li *ngFor="let item of items">{{ item }}</li><!-- With index --><li *ngFor="let item of items; index as i"> {{ i + 1 }}. {{ item }}</li><!-- With tracking function --><li *ngFor="let item of items; trackBy: trackByFn"> {{ item.name }}</li><!-- With local variables --><li *ngFor="let item of items; first as isFirst; last as isLast; even as isEven"> <span *ngIf="isFirst">First: </span> {{ item }} <span *ngIf="isLast"> (Last)</span> <span *ngIf="isEven"> [Even]</span></li>
The following variables are available in the template context:
$implicit: T - The current item
ngForOf: NgIterable<T> - The collection being iterated
index: number - Current index (0-based)
count: number - Total number of items
first: boolean - True if first item
last: boolean - True if last item
even: boolean - True if even index
odd: boolean - True if odd index
<!-- Old (Deprecated) --><li *ngFor="let item of items; trackBy: trackByFn"> {{ item }}</li><!-- New (Recommended) -->@for (item of items; track item.id) { <li>{{ item }}</li>}