In Angular, directives are special markers in the DOM that tell Angular to do something to that DOM element (or even the component). Built-in directives are provided by Angular to help you manipulate the DOM easily. This section will cover the most commonly used built-in directives: ngIf, ngFor, ngSwitch, ngClass, and ngStyle.

Key Concepts

  1. Directives: Instructions in the DOM that tell Angular to perform specific actions.
  2. Structural Directives: Change the structure of the DOM by adding or removing elements (ngIf, ngFor, ngSwitch).
  3. Attribute Directives: Change the appearance or behavior of an element (ngClass, ngStyle).

Structural Directives

ngIf

The ngIf directive conditionally includes or excludes an element in the DOM based on a boolean expression.

Example:

<div *ngIf="isVisible">This text is visible only if isVisible is true.</div>

Explanation:

  • *ngIf="isVisible": The element will be included in the DOM only if isVisible is true.

ngFor

The ngFor directive is used to repeat a portion of the DOM tree for each item in a list.

Example:

<ul>
  <li *ngFor="let item of items">{{ item }}</li>
</ul>

Explanation:

  • *ngFor="let item of items": Iterates over the items array and creates a <li> element for each item.

ngSwitch

The ngSwitch directive is used to conditionally display one of several elements based on a switch expression.

Example:

<div [ngSwitch]="color">
  <div *ngSwitchCase="'red'">Red</div>
  <div *ngSwitchCase="'blue'">Blue</div>
  <div *ngSwitchDefault>Other</div>
</div>

Explanation:

  • [ngSwitch]="color": The switch expression.
  • *ngSwitchCase="'red'": Displays this element if color is 'red'.
  • *ngSwitchDefault: Displays this element if no other case matches.

Attribute Directives

ngClass

The ngClass directive adds or removes CSS classes on an element based on an expression.

Example:

<div [ngClass]="{ 'active': isActive, 'disabled': isDisabled }">Styled Div</div>

Explanation:

  • [ngClass]="{ 'active': isActive, 'disabled': isDisabled }": Adds the active class if isActive is true and the disabled class if isDisabled is true.

ngStyle

The ngStyle directive adds or removes inline styles on an element based on an expression.

Example:

<div [ngStyle]="{ 'color': textColor, 'font-size': fontSize + 'px' }">Styled Text</div>

Explanation:

  • [ngStyle]="{ 'color': textColor, 'font-size': fontSize + 'px' }": Sets the color style to textColor and the font-size style to fontSize in pixels.

Practical Exercises

Exercise 1: Using ngIf

Task: Create a button that toggles the visibility of a paragraph using ngIf.

Solution:

<button (click)="isVisible = !isVisible">Toggle Paragraph</button>
<p *ngIf="isVisible">This paragraph is conditionally visible.</p>

Explanation:

  • The button toggles the isVisible variable.
  • The paragraph is displayed only if isVisible is true.

Exercise 2: Using ngFor

Task: Display a list of items using ngFor.

Solution:

<ul>
  <li *ngFor="let item of items">{{ item }}</li>
</ul>

Explanation:

  • Iterates over the items array and creates a <li> element for each item.

Exercise 3: Using ngClass

Task: Add a class to a div based on a condition using ngClass.

Solution:

<div [ngClass]="{ 'highlight': isHighlighted }">Conditionally Styled Div</div>

Explanation:

  • Adds the highlight class if isHighlighted is true.

Common Mistakes and Tips

  • Common Mistake: Forgetting to use the asterisk (*) with structural directives like ngIf and ngFor.

    • Tip: Always use the asterisk (*) when using structural directives to indicate that the directive is modifying the structure of the DOM.
  • Common Mistake: Using incorrect syntax for ngClass and ngStyle.

    • Tip: Ensure that the expressions inside ngClass and ngStyle are valid objects or arrays.

Conclusion

Built-in directives in Angular are powerful tools that allow you to manipulate the DOM easily. Understanding and using these directives effectively can greatly enhance the functionality and interactivity of your Angular applications. In the next section, we will explore custom directives, which allow you to create your own reusable directives to further extend Angular's capabilities.

© Copyright 2024. All rights reserved