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 Angular Focusout Event on Input Example

Angular Focusout Event on Input Example

September 24, 2022March 16, 2024

Angular Focusout Event on Input is an Angular event binding it triggers when user interacts with DOM like text based input element and focus get out from the element. Input events in angular can be handled using input and output decorators and in this tutorial we will learn Angular FocusOut…

Read More
Javascript Laravel Dynamic Autocomplete Using Typehead JS

Laravel Dynamic Autocomplete Using Typehead JS

July 16, 2022July 16, 2022

In this article we will learn to use Laravel Dynamic Autocomplete Using Typehead JS. Typehead JS is useful when we want live search of bulk data. Autocomplete search is mostly work of javascript and when we want to fetch live data from database then we require the intervention of laravel…

Read More

How to create a service in angular?

September 11, 2021October 3, 2022

Services are same as class with a specific purpose of it. Services can contain method, property and any feature of an application which we can reuse multiple time in our web application. Services also contain a decorator which tell angular how to consume it in our application. Let’s create a…

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

  • 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

  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
  • Understanding High Vulnerabilities in Software: A Week of Insights
  • Blocking Spam Requests with LaraGuard IP: A Comprehensive Guide
  • Enhancing API Development with Laravel API Kit
  • Exploring the Future of Web Development: Insights from Milana Cap
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version