Welcome to our Angular tutorial! Today, we're going to dive into three powerful Angular directives: ngIf, ngFor, and ngSwitch. These directives will help you manipulate and control the content of your Angular applications dynamically.
By the end of this tutorial, you'll understand:
ngIf for conditional renderingngFor for looping through collectionsngSwitch for multiple conditional renderingLet's get started!
Directives are a way to extend the functionality of HTML in Angular. They let you add new attributes, elements, and behavior to HTML. ngIf, ngFor, and ngSwitch are some of the most commonly used directives.
š” Pro Tip: ngIf is used for conditional rendering, meaning it can hide or show HTML elements based on a condition.
<div *ngIf="condition">
This div will be displayed if the condition is true.
</div>Quiz
What does the `*ngIf` directive do?
Example
Let's create a simple example where we show a "Welcome" message if a user is logged in.
import { Component } from '@angular/core';
@Component({
selector: 'app-welcome',
template: `
<h1 *ngIf="isLoggedIn">Welcome, User!</h1>
<h1 *ngIf="!isLoggedIn">Please log in to continue.</h1>
`
})
export class WelcomeComponent {
isLoggedIn = false;
}š Note: In the example above, we have created a WelcomeComponent. The isLoggedIn variable determines whether the user is logged in or not. Depending on the value of isLoggedIn, we use *ngIf to show either a "Welcome, User!" or "Please log in to continue." message.
š” Pro Tip: ngFor is used for looping through collections and displaying them in the HTML.
<ul>
<li *ngFor="let item of items">
{{ item }}
</li>
</ul>Quiz
What does the `*ngFor` directive do?
Example
Let's create a simple example where we display a list of fruits.
import { Component } from '@angular/core';
@Component({
selector: 'app-fruits',
template: `
<ul>
<li *ngFor="let fruit of fruits">
{{ fruit }}
</li>
</ul>
`,
})
export class FruitsComponent {
fruits = ['Apple', 'Banana', 'Orange'];
}š Note: In the example above, we have created a FruitsComponent. We define an array of fruits and use *ngFor to loop through them and display each fruit in an HTML list.
š” Pro Tip: ngSwitch is used for multiple conditional rendering based on different possible values.
<div [ngSwitch]="status">
<div *ngSwitchCase="'success'">Success!</div>
<div *ngSwitchCase="'error'">Error!</div>
<div *ngSwitchDefault>Unknown status.</div>
</div>Quiz
What does the `[ngSwitch]` directive do?
Example
Let's create a simple example where we display a message based on a status.
import { Component } from '@angular/core';
@Component({
selector: 'app-status',
template: `
<div [ngSwitch]="status">
<div *ngSwitchCase="'success'">Success!</div>
<div *ngSwitchCase="'error'">Error!</div>
<div *ngSwitchDefault>Unknown status.</div>
</div>
`,
})
export class StatusComponent {
status = 'success';
}š Note: In the example above, we have created a StatusComponent. We use [ngSwitch] to switch between different HTML elements based on the value of status. Depending on the value of status, the appropriate message will be displayed.
That's it for today! You've learned how to use ngIf, ngFor, and ngSwitch in Angular. These directives will help you manipulate and control the content of your Angular applications dynamically. Happy coding! šÆ