Angular ngSwtich
Last Updated on May 20, 2022Angular ngSwitch
In Angular ngSwitch built-in structural directive also allows us to add or remove the HTML element based on multiple conditions just like a ngIf directive. The ngSwitch directive works just like a switch statement in other programming languages.
Let’s look at code syntax and some code examples of using ngSwitch.
...
...
...
render if nothing match from above condtion
Let's take a look at the code example.
import { Component } from '@angular/core';
@Component({
selector: 'app-demoComponent',
template: `
nRandomNum value is 0
nRandomNum value is 1
nRandomNum value is 2
nRandomNum value is 3
nRandomNum value is 4
nRandomNum value is 5
nRandomNum value doesn't match between 0 - 5
`
})
export class DemoComponent {
nRandomNum = 10;
constructor(){
//Constructor
}
}
As an output, based on the above code we should be able to see the default statement.