Wednesday 29 November 2017

ng switch in Angular


Let us check how ngSwitch works in Angular. Basically it is almost similar to Angular js 1 except some syntax changes. Let us check how it is in Angular js 1 first.

ng-switch in Angular js 1

$ = data; // json or backend data

<div ng-switch="data.status">
 <span ng-switch-when="C">Initializing</span>
 <span ng-switch-when="J">Loading</span>
 <span ng-switch-when="R">Creating</span>
 <span ng-switch-when="D">Success</span>
 <span ng-switch-when="P">Failed</span>


Now let us check how it is in Angular 4.

ngSwitch in Angular 4 = data; // json or backend data

<div [ngSwitch]="data.status">
 <span *ngSwitchCase="'C'">Initializing</span>
 <span *ngSwitchCase="'J'">Loading</span>
 <span *ngSwitchCase="'R'">Creating</span>
 <span *ngSwitchCase="'D'">Success</span>
 <span *ngSwitchCase="'P'">Failed</span>
 <span *ngSwitchDefault>Error</span>


In this article we have discussed about using ngSwitch in Angular.


Related Info

1. ng if in Angular 4.

2. ng if in Angular 2.

3. ng repeat in Angular.

4. ng click in Angular.

No comments :

Post a Comment