AngularJS Check Null Empty or Undefined Variable Value Example
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
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. 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, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Get Current Date and Time in AngularJS?
- AngularJS Convert String to Object Example
- AngularJS Filter Change Date Format in Controller Example
- AngularJS Remove Duplicates Object from Array Example
- AngularJS Check and Uncheck All Checkboxes Example
- AngularJS Check Checkbox is Checked or Not Example
- AngularJS - How to Create Read More/Less Toggle using Directive?
- AngularJS Nl2br Filter for Textarea Content Example
- AngularJS Image Upload with Preview Example
- How to Hide Div After Some Time in AngularJS?
- How to Call Function on Page Load in AngularJS?
- AngularJS Simple Datepicker Directive Example Tutorial
- AngularJS - How to Limit String Length using Filter?
- AngularJS - How to Capitalize the First Letter of Word Example
- How to Remove HTML Tags from String in AngularJS?