×

ngSwitch Directive in Angular 8

Angular 8 ng-Switch Directive

The ng-Switch Directive hides and shows the HTML elements depending on an expression.

Child elements with the ng-switch-when directive will be displayed if it gets a match; otherwise, the component and its children will be removed.

We could also define a default section, by using the ng-switch default directive, to show a section if no any other part gets a match.

Syntax:


 
 
 
 
 

ng-SwitchCase

In Angular ngSwitchCase directive, the inner elements are placed inside the container element. It is applied to the inner elements with a match expression. When the value of the match expression matches the value of switch expression, the correlated inner element is added in the DOM. All other inner elements are removed from the DOM.

If there is more than one match, then all the matching elements are added to the DOM.

ng-SwitchDefault

We can apply the ng-switchDefault directive in Angular 8. The element with ngSwitchDefault is displayed if no match found. The inner element with ngSwitchDefault can be placed anywhere inside the container element and not necessarily at the bottom line. If we add more than ngSwitchDefault directive, and all of them are displayed.

Any elements placed inside the container element, but outside the ngSwitchCase or ngSwitchDefault elements are displayed the same as it is.

ngSwitch Directive Example

Write the given code in app.component.ts file of the application:

class item {
name: string;
val: number; 
}
export class AppComponent 
{
Items: item[ ]=[{name: ’one’,val: 1},
{ name: ’two’,val: 2},{name: ‘three’, val: 3}];
selectedValue: string=’one’; 
} 

Use the below code in the app.component.html file of the application.

 
One is pressed
Two is pressed
;Default Option

Related Topics

Angular 8 Tutorial

What is Angular 8 Angular is a Framework of JavaScript used to build web and mobile applications. Angular 8 is a client-side TypeScript based structure which is used to create dynamic...

6 minutes read.

Difference between Angular and React

Difference between Angular and React Parameters AngularReactType Angular is a complete framework. React is a JavaScript library, and much older compared...

2 minutes read.

Angular 8 Libraries

Angular libraries are built a solution of general problems like presenting a unified user interface, presenting data, and allowing the data entry. Developers can create standard solutions for particular domains...

3 minutes read.

Angular 8 NgStyle Directive

Angular 8 NgStyle Directive The ngStyle attribute is used to change and style the multiple properties of Angular. We can change the value, color, and size, etc. of the component. It is a built-in...

1 minute read.

Difference between Node.js and ASP.net

Introduction to Node.js V8 (the JavaScript engine operating inside Google Chrome) is packaged with certain libraries and is mostly used for input & output, which includes creating data and managing connection...

4 minutes read.

String Interpolation in Angular 8

String Interpolation in Angular 8 String interpolation is a one-way data-binding technique which is used to output the data from a typescript code to HTML template. It uses the template expression. It uses the...

2 minutes read.

Creating form in Angular 8

The angular reactive form is used to handle the user's input. We can use Angular form in the application to authorize the user to log in, to update profile, to enter information,...

2 minutes read.

Angular 8 Module

Angular 8 Module It is a collection of services, directives, controllers, filters, and configuration information. angular.module is used to configure the $injector. The module is a container of the different parts of an application....

6 minutes read.

Express.js app.listen() Function

Express or Express.js is a flexible online application framework for Node.js that offers a strong set of functionality for both web and mobile applications. While preserving the familiar and adored...

3 minutes read.

Angular 8 Components

Angular is used for building mobile and desktop web applications. The component is the basic building block of Angular. It has a selector, template, style, and other properties, and it...

4 minutes read.

Angular 8 *ngFor Directive

Angular 8ngFor Directive The *ngFor directive is used to repeat to repeat a portion of HTML template once per each item from an iterable list (collection). The ngFor is an Angular...

2 minutes read.

What is AOT and JIT Compiler in Angular

A program written in a high-level language is translated into machine code or a low-level language by a piece of software called a compiler. HTML templates are the primary building...

3 minutes read.

Angular CLI Commands

All CLI commands of Angular Angular CLI is a command-line interface which is used to initialize, develop, and maintain Angular applications. We can use these Command on command prompt or consequentially by an associated...

8 minutes read.

Angular 8 File Structure

File Structure in Angular 8: A file structure contains the files for one or more projects. A project is the set of files that comprise an application on a shareable...

3 minutes read.

Angular 8 ngClass Directive

Angular 8 ngClass Directive ngClass Directive is a type of attribute directive. Angular provided built-in directive, and it helps in adding or removing CSS classes on an HTML element. The ngClass directive allows...

2 minutes read.

Data Binding in Angular 8

What is Binding? Binding is the process which generates the connection between the application UI and the data which comes from the business logic. In Angular, it is called the automatic synchronization of...

3 minutes read.

Deployment of an Angular 8 app

Deploying an Angular 8 app To implement our application, we have to compile it and then host the JavaScript, CSS, and HTML on a web server. Build an Angular application which is very portable...

4 minutes read.

Angular 8 with Bootstrap

How to install Bootstrap for Angular? Run the following Command in Command prompt. npm install–save bootstrap@3= > The @3 is essential! After that, when we use a project created with Angular CLI 6+, we will...

1 minute read.

Installation of Angular 8

Installation Guides to Angular 8 step by Step Angular combines declarative templates, dependency injection and, end to end tooling to solve development challenges. Angular empowers developers to build an application that lives on the...

4 minutes read.

Property binding in Angular 8

Property binding is the primary way to binding data in Angular.  To bind data to a property of any element, we use square braces[ ]. It is also a one-way data-binding...

2 minutes read.