Structural Directives

5. A. AIM: Illustrate structural directive ngIf-else.

Step-1: Edit the app.component.html with the following code.

app.component.html
<div *ngIf="true; else falseBlock">
  This is if block text.
</div>
<ng-template #falseBlock>
  This is else block text.
</ng-template>

Step-2

Run the following command to build and serve your app.

.../first-app> ng serve

Open browser and enter the URL http://localhost:4200 to find the application.

Output:

5. B. AIM: Create a courses array and rendering it in the template using ngFor directive in a list format.

Step-1: Edit the app.component.ts with the following code.

app.component.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  courses = ['HTML','CSS','JavaScript','Angular','React']
}

Step-2: Edit the app.component.html with the following code.

app.component.html
<div>
  <h3>List of Courses:</h3>
  <ul>
    <li *ngFor = "let course of courses">{{ course }}</li>
  </ul>
</div>

Step-3

Run the following command to build and serve your app.

.../first-app> ng serve

Open browser and enter the URL http://localhost:4200 to find the application.

Output:

5. C. AIM: Display the correct option based on the value passed to ngSwitch directive.

Step-1: Edit the app.component.html with the following code.

app.component.html
<div [ngSwitch]="3">
  <label *ngSwitchCase="1">HTML</label>
  <label *ngSwitchCase="2">CSS</label>
  <label *ngSwitchCase="3">JavaScript</label>
  <label *ngSwitchCase="4">Angular</label>
  <label *ngSwitchDefault>No Course Selected</label>
</div>

Step-2

Run the following command to build and serve your app.

.../first-app> ng serve

Open browser and enter the URL http://localhost:4200 to find the application.

Output: