Check null empty or undefined Angularjs Example

Check null empty or undefined Angularjs Example

In this post we will show you Check null empty or undefined Angularjs Example, hear for Check or null or empty or undefined Angularjs Example we will give you demo and example for implement.

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.

Check null empty or undefined Angularjs Example

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     </title>

         <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscript%20src%20%3D%20%22http%3A%2F%2Fajax.googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.3.14%2Fangular.min.js%22%3E%20%20%20%20%20%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="&lt;script&gt;" title="&lt;script&gt;" />
     </head>
     <body>  

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

  

     <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-wp-preserve="%3Cscript%20type%3D%22text%2Fjavascript%22%3E%0A%09%20var%20app%20%3D%20angular.module(%22mainApp%22%2C%20%5B%5D)%3B%20%20%0A%09%20app.controller('myController'%2C%20function(%24scope%2C%20%24timeout)%20%7B%20%20%0A%09%09%20%20%24scope.myNullVar%20%3D%20null%3B%0A%09%09%20%20if%20(!%24scope.myNullVar)%20%7B%0A%09%09%09%20%20console.log('variable%20is%20a%20null.')%3B%0A%09%09%20%20%7Delse%7B%0A%09%09%09%20%20console.log('variable%20is%20a%20not%20null.')%3B%0A%09%09%20%20%7D%20%20%20%0A%09%09%20%20%24scope.myEmptyVar%20%3D%20''%3B%0A%09%09%20%20if%20(!%24scope.myNullVar)%20%7B%0A%09%09%09%20%20console.log('variable%20is%20a%20empty.')%3B%0A%09%09%20%20%7Delse%7B%0A%09%09%09%20%20console.log('variable%20is%20a%20not%20empty.')%3B%0A%09%09%20%20%7D%0A%0A%09%09%20%20if%20(!%24scope.myUndefinedVar)%20%7B%0A%09%09%09%20%20console.log('variable%20is%20a%20undefined.')%3B%0A%09%09%20%20%7Delse%7B%0A%09%09%09%20%20console.log('variable%20is%20a%20not%20undefined.')%3B%0A%09%09%20%20%7D%0A%09%20%7D)%3B%0A%20%20%20%20%20%3C%2Fscript%3E" data-mce-resize="false" data-mce-placeholder="1" class="mce-object" width="20" height="20" alt="&lt;script&gt;" title="&lt;script&gt;" />
     </body>
</html>

Output:

Read Also: How to get selected radio button value in AngularJS?

variable is a null.
variable is a empty.
variable is a undefined.

Hope this code and post will help you to implement Check empty or undefined Angularjs Example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us to help you more and improve onlincode. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs onlincode.org

Leave a Comment

Your email address will not be published. Required fields are marked *

9  +  1  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US