iCode
iCode

Reputation: 9202

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?

Upvotes: 2

Views: 23638

Answers (2)

Cheezy Code
Cheezy Code

Reputation: 1715

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

Upvotes: 1

godfrzero
godfrzero

Reputation: 2270

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.

Upvotes: 10

Related Questions