Directives
Directives
Section titled βDirectivesβWhat it means
Section titled βWhat it meansβA directive attaches behavior to a DOM element. Angular has three kinds: components (directives with a template), structural directives (change the DOMβs structure β add/remove elements, like *ngIf/*ngFor, or the modern built-in control flow @if/@for), and attribute directives (change an elementβs appearance/behavior without adding/removing it, like ngClass/ngStyle).
Examples
Section titled βExamplesβ@Component({ selector: 'app-list', template: ` <!-- Modern built-in control flow (Angular 17+), recommended for new code --> @if (items.length > 0) { @for (item of items; track item.id) { <p>{{ item.name }}</p> } } @else { <p>No items</p> }
<!-- Older structural directive syntax, still widely seen in existing codebases --> <p *ngIf="items.length > 0">Has items</p> <p *ngFor="let item of items; trackBy: trackById">{{ item.name }}</p>
<!-- Attribute directive --> <div [ngClass]="{ active: isActive, disabled: !isEnabled }">Status</div> `,})export class ListComponent { items = [{ id: 1, name: 'Apple' }]; isActive = true; isEnabled = true; trackById(index: number, item: { id: number }) { return item.id; }}Common mistake
Section titled βCommon mistakeβUsing *ngFor without a track/trackBy on a list that changes over time β without it, Angular canβt tell which items are βthe sameβ between renders, so it may destroy and recreate DOM elements unnecessarily (losing input focus, animation state, etc.) instead of reusing them.
<!-- No tracking -- Angular may re-render every item on any change to the array --><div *ngFor="let item of items">{{ item.name }}</div>
<!-- Modern control flow requires track and forces you to think about it -->@for (item of items; track item.id) { <div>{{ item.name }}</div>}Quick practice
Section titled βQuick practiceβ-
What are the three kinds of Angular directives?
Answer
Components (directives with a template), structural directives (change DOM structure, like*ngIf/@if), and attribute directives (change appearance/behavior without adding/removing elements, likengClass). -
Whatβs the modern, built-in alternative to
*ngFor/*ngIfintroduced in Angular 17?Answer
The block-based control flow syntax β@for,@if,@switchβ which requires no imports and mandates atrackexpression on@for. -
Why does skipping
trackBy/trackon a repeated list hurt performance?Answer
Without it, Angular can't identify which items persisted between renders, so it may unnecessarily destroy and recreate DOM nodes for unchanged items instead of reusing them.