Check null empty or undefined Angularjs Example

By Hardik Savani | September 25, 2019 | | 9320 Viewer | Category : AngularJS


you don't know how to check variable or object is null empty or undefined in angular js then i would like to help you to check undefined or null variable or object in angularjs. we can determine easily to variable is empty or not in angularjs.

Few months ago one developer send me email and he say me to i am working on my angularjs application and i need to check variable value is empty or null then both how can i do it. he also send me some code that he tried out. actually his code is also was working but i said him this is not way to you are checking object is empty, null or undefined in angularjs.

I said him and also send him solution that i am sharing with you. It's very simple and easy way we can do it using code jquery logic.

So basically, you can check it as like bellow code, i also write full example so you can check and see how it works.

if (!$scope.myVar) {

// If variable is empty, null and undefined

}else{

// If not

}

Example:

<!DOCTYPE html>

<html>

<head>

<title>Check null empty or undefined Angularjs Example - ItSolutionStuff.com</title>

<script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>

</head>

<body>

<div ng-app="mainApp" ng-controller="myController">

</div>

<script type="text/javascript">

var app = angular.module("mainApp", []);

app.controller('myController', function($scope, $timeout) {

$scope.myNullVar = null;

if (!$scope.myNullVar) {

console.log('variable is a null.');

}else{

console.log('variable is a not null.');

}

$scope.myEmptyVar = '';

if (!$scope.myNullVar) {

console.log('variable is a empty.');

}else{

console.log('variable is a not empty.');

}

if (!$scope.myUndefinedVar) {

console.log('variable is a undefined.');

}else{

console.log('variable is a not undefined.');

}

});

</script>

</body>

</html>

Output:

variable is a null.

variable is a empty.

variable is a undefined.

I hope it can help you...




Hardik Savani
My name is Hardik Savani. I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.
Follow Me: Github Twitter
***Do you want me hire for your Project Work? Then Contact US.

We are Recommending you: