ItSolutionStuff.com

Angular Check ngIf Null or Empty | Angular Check If Array is Empty

By Hardik Savani β€’ May 2, 2024
Angular

Hi Dev,

This tutorial will provide example of how to check array is empty in angular. i would like to show you angular check if string is empty or null. This article will give you simple example of angular check if object is empty ngif. it's simple example of angular check ngif array is empty. Alright, let’s dive into the steps.

In this post, i will give three examples with checking condition with ngIf in angular. You can easily check with angular 6, angular 7, angular 8, angular 9, angular 10, angular 11, angular 12, angular 13, angular 14, angular 15, angular 16 and angular 17.

1) Angular Check ngIf Null or Not

2) Angular Check ngIf String is Empty or Not

3) Angular Check ngIf Array is Empty or Not

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>

I hope it can help you...

Tags: Angular
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

πŸ“Ί Subscribe on YouTube

We Are Recommending You

β˜…

Angular NgIf Else | Ng If Else in Angular Example

Read Now β†’
β˜…

Angular Material Radio Button Example

Read Now β†’
β˜…

Angular 9 Toastr Notifications Example

Read Now β†’
β˜…

Angular Get Active Route URL Example

Read Now β†’
β˜…

How to Install And Use JQuery in Angular?

Read Now β†’
β˜…

How to Redirect to Another Page in Angular?

Read Now β†’
β˜…

How to Set Style Dynamically in Angular 8?

Read Now β†’