Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check null,empty or undefined angularjs

Tags:

angularjs

I am creating a project using angularjs.I have variable like

$scope.test = null $scope.test = undefined $scope.test = "" 

I want to check all null,undefined and empty value in one condition

like image 238
user5798214 Avatar asked Mar 21 '16 07:03

user5798214


People also ask

How check variable is empty or not in Angularjs?

You can use angular's function called angular. isUndefined(value) returns boolean. Show activity on this post. if(!a) { // do something when `a` is not undefined, null, ''. }

Is null or empty in angular?

How to check if a variable string is empty or undefine or null in Angular. In template HTML component: We can use the ngIf directive to check empty null or undefined. In this example, if stringValue is empty or null, or undefined, It prints the empty message.


2 Answers

just use -

if(!a) // if a is negative,undefined,null,empty value then... {     // do whatever } else {     // do whatever } 

this works because of the == difference from === in javascript, which converts some values to "equal" values in other types to check for equality, as opposed for === which simply checks if the values equal. so basically the == operator know to convert the "", null, undefined to a false value. which is exactly what you need.

like image 190
Ran Sasportas Avatar answered Sep 22 '22 15:09

Ran Sasportas


You can do

if($scope.test == null || $scope.test === ""){   // null == undefined } 

if false, 0 and NaN can also be considered as false values you can just do

if($scope.test){  //not any of the above } 
like image 39
T J Avatar answered Sep 22 '22 15:09

T J