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
- Directives: Instructions in the DOM that tell Angular to perform specific actions.
- Structural Directives: Change the structure of the DOM by adding or removing elements (
ngIf
,ngFor
,ngSwitch
). - 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:
Explanation:
*ngIf="isVisible"
: The element will be included in the DOM only ifisVisible
istrue
.
ngFor
The ngFor
directive is used to repeat a portion of the DOM tree for each item in a list.
Example:
Explanation:
*ngFor="let item of items"
: Iterates over theitems
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 ifcolor
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:
Explanation:
[ngClass]="{ 'active': isActive, 'disabled': isDisabled }"
: Adds theactive
class ifisActive
istrue
and thedisabled
class ifisDisabled
istrue
.
ngStyle
The ngStyle
directive adds or removes inline styles on an element based on an expression.
Example:
Explanation:
[ngStyle]="{ 'color': textColor, 'font-size': fontSize + 'px' }"
: Sets thecolor
style totextColor
and thefont-size
style tofontSize
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
istrue
.
Exercise 2: Using ngFor
Task:
Display a list of items using ngFor
.
Solution:
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:
Explanation:
- Adds the
highlight
class ifisHighlighted
istrue
.
Common Mistakes and Tips
-
Common Mistake: Forgetting to use the asterisk (
*
) with structural directives likengIf
andngFor
.- Tip: Always use the asterisk (
*
) when using structural directives to indicate that the directive is modifying the structure of the DOM.
- Tip: Always use the asterisk (
-
Common Mistake: Using incorrect syntax for
ngClass
andngStyle
.- Tip: Ensure that the expressions inside
ngClass
andngStyle
are valid objects or arrays.
- Tip: Ensure that the expressions inside
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.
Angular Course
Module 1: Introduction to Angular
- What is Angular?
- Setting Up the Development Environment
- Angular Architecture
- First Angular Application
Module 2: Angular Components
- Understanding Components
- Creating Components
- Component Templates
- Component Styles
- Component Interaction
Module 3: Data Binding and Directives
- Interpolation and Property Binding
- Event Binding
- Two-Way Data Binding
- Built-in Directives
- Custom Directives
Module 4: Services and Dependency Injection
Module 5: Routing and Navigation
Module 6: Forms in Angular
Module 7: HTTP Client and Observables
- Introduction to HTTP Client
- Making HTTP Requests
- Handling HTTP Responses
- Using Observables
- Error Handling
Module 8: State Management
- Introduction to State Management
- Using Services for State Management
- NgRx Store
- NgRx Effects
- NgRx Entity
Module 9: Testing in Angular
Module 10: Advanced Angular Concepts
- Angular Universal
- Performance Optimization
- Internationalization (i18n)
- Custom Pipes
- Angular Animations