Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Angular If Else

How to use NgIf, Ngif else and elseif in angular 12 ?

Aman Jain, April 2, 2022November 7, 2023

In this tutorial we will learn to use angular if else, ngIf, ngif else and elseif. if else is used to make conditional statement in template same as we can also use if else. As we all programmers know if or if else condition is used to create conditional statements so in angular template we can also use the if else but for elseif there is no direct way but we can use alternate way.

ngIf is a structural directive which is used to control the layout of dom by adding and removing the content of dom. You can read more about structural directive here What is directives and types of directive in angular?

Syntax of angular if else

Here is the syntax of *ngIf

<ng-container *ngIf="count == 1"></ng-container>

Syntax of *ngIf else

<ng-container *ngIf="count === 1;else second"></ng-container>
<ng-template #second>
    <ng-container *ngIf="count === 2"></ng-container>
</ng-template>

Syntax of *ngIf elseif

<ng-container *ngIf="count === 1;else second"></ng-container>
<ng-template #second>
    <ng-container *ngIf="count === 2;else third"></ng-container>
</ng-template>
<ng-template #third></ng-template>

You can also achieve the same as below

<ng-container *ngIf="count === 1;"></ng-container>
<ng-container *ngIf="count === 2"></ng-container>
<ng-container *ngIf="count<0"></ng-container>

Let’s understand use NgIf, if else and elseif with example

Step 1 : Create an angular app

First step is to setup an angular app using below command

ng new example-component

Step 2: Create a Component

Create a component so we can add conditional statements

ng g c test-condition
import { Component, OnInit, Input } from '@angular/core';

@Component({
  selector: 'app-test-condition',
  templateUrl: './test-condition.component.html',
  styleUrls: ['./test-condition.component.css']
})
export class TestConditionComponent implements OnInit {
    count:number =1;

  constructor() {

    setInterval(()=>{
      this.count++;
    },2000)
   
  }


}

Step 3 : Create template and add conditional *ngIf

Now, add simple html template with *ngif

<h1>use NgIf if else and elseif in angular- ReaderStacks</h1>
<h2>Example 1</h2>
<ng-container *ngIf="count === 1;else second"> Condition 1 </ng-container>
<ng-template #second>
    <ng-container *ngIf="count === 2;else third" >Condition 2 </ng-container>
</ng-template>
<ng-template #third> Condition 3</ng-template>


<h2>Example 2</h2>
<!-- count = 5 -->
<ng-container *ngIf="count >= 1 && count <= 3; then elseif3"></ng-container>
<ng-container *ngIf="count >= 4 && count <= 6; then elseif4"></ng-container>
<ng-container *ngIf="count >= 7; then elseif5"></ng-container>

<!-- If Statement -->
<ng-template #elseif3>
    count between 1 and 3
</ng-template>
<!-- If Else Statement -->
<ng-template #elseif4>
     count between 4 and 6
</ng-template>
<!-- Else Statement -->
<ng-template #elseif5>
     count greater than 7
</ng-template>


Live Example and code:

Stack blitz : https://stackblitz.com/edit/angular-ivy-jrvtth

Related

Javascript Angular angularelsifngif

Post navigation

Previous post
Next post

Related Posts

Javascript Min Length and Max Length Validation in Angular 12 13

Min Length and Max Length Validation in Angular 12 / 13 ?

March 21, 2022March 21, 2022

Angular provides built-in library for validation, in the same way it gives validation methods to validate the string and numbers length as well. so in this tutorial i am going to explain how to use min length and max length validation in angular. In angular validation library it provides different…

Read More
Javascript Remove hash from url in Angular

Remove hash from URL in angular 12 and deploy to server

February 1, 2022

From the first install angular come with hash URL routing and it’s defined in app-routing.module.ts. To remove hash from URL we need to change useHash key to false. Angular runs it development application ng server and whenever we wanted to deploy the application we need a server like Apache or…

Read More
Javascript custom validation in angular reactive form

How to make custom validation in angular reactive form ?

September 7, 2021October 1, 2021

Angular already have many built-in validators but sometimes we need some extra validation according to our project need. Steps to create custom validators Step 1: Create a function or class method where we can define our all custom validators. File: src/CustomValidators.ts 2. Define the function definition of custom validators to…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version