Hello Devs, 

In this tutorial, we will learn How to Check Array is Empty in Angular

This post will give you simple example of ngif check if string is empty. Here, Creating a basic example of ngif check length of array angular.

Follow this step by step guide below. 



1) Angular Check ngIf Null or Not:

src/app/app.component.ts

import { Component } from '@angular/core';

  

@Component({

  selector: 'my-app',

  templateUrl: './app.component.html',

  styleUrls: [ './app.component.css' ]

})

export class AppComponent  {

  name = 'Angular';

  

  myVar = null;

}


src/app/app.component.html

<h1>angular ngif check null example - itsolutionstuff.com</h1>

  

<div *ngIf="myVar">

  <p>Variable is not null.</p>

</div>

  

<div *ngIf="!myVar">

  <p>Variable is null.</p>

</div>


2) Angular Check ngIf String is Empty or Not:

src/app/app.component.ts

import { Component } from '@angular/core';

  

@Component({

  selector: 'my-app',

  templateUrl: './app.component.html',

  styleUrls: [ './app.component.css' ]

})

export class AppComponent  {

  name = 'Angular';

  

  myVar = "";

}


src/app/app.component.html

<h1>angular ngif check empty example - itsolutionstuff.com</h1>

  

<div *ngIf="myVar">

  <p>Variable is not empty.</p>

</div>

    

<div *ngIf="!myVar">

  <p>Variable is empty.</p>

</div>


3) Angular Check ngIf Array is Empty or Not

src/app/app.component.ts

import { Component } from '@angular/core';

  

@Component({

  selector: 'my-app',

  templateUrl: './app.component.html',

  styleUrls: [ './app.component.css' ]

})

export class AppComponent  {

  name = 'Angular';

  

  myVar = [];

}


src/app/app.component.html

<h1>angular ngif check array empty example - itsolutionstuff.com</h1>

  

<div *ngIf="myVar?.length">

  <p>Array is not empty.</p>

</div>

  

<div *ngIf="!myVar?.length">

  <p>Array is empty.</p>

</div>


May this example help you.