Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS- Check A value is undefined or null

Okay! New to AngularJS. Here is my controller code to check if a value is undefined or null.

var configId=$routeParams.confiId;
    angular.isUndefinedOrNull = function(configId){ return angular.isUndefined(configId) || configId === null};
    if(angular.isUndefinedOrNull) {
        alert(configId);
        alert("true");
    } else {
        alert("false");
    }

And it always alerts with true. So I tried alerting configId. If the value is there, it alerts the value otherwise it is alerting undefined. Not going to else part as condition is always true. What is wrong here?

like image 425
iCode Avatar asked Jul 30 '26 18:07

iCode


2 Answers

if(angular.isUndefinedOrNull) is basically checking to see if angular.isUndefinedOrNull is truthy. Since angular.isUndefinedOrNull is a function, this will always be true.

Try if( angular.isUndefinedOrNull(configId) ), which checks if the value returned by angular.isUndefinedOrNull is truthy. This will alert true if the value returned by angular.isUndefinedOrNull() is truthy, and false otherwise.

like image 58
godfrzero Avatar answered Aug 01 '26 09:08

godfrzero


You can use basic javascript check like

if(nameOfVariable) //Returns false if variable has null or blank or undefined as value
{

} 
else
{

}

Cheers from CheezyCode

like image 27
Cheezy Code Avatar answered Aug 01 '26 09:08

Cheezy Code



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!