2

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?

3
  • try if(angular.isUndefinedOrNull(configId)) instead Commented Feb 8, 2014 at 6:11
  • No. It is also not working Commented Feb 8, 2014 at 6:14
  • Sorry @KhanhTO it was working. I only replaced the if and alerted wrong. :) Commented Feb 8, 2014 at 6:18

2 Answers 2

10

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.

Sign up to request clarification or add additional context in comments.

5 Comments

To add to @godfrzero's answer here, you also want to be aware that function(configId) {...} isn't passing configId in, it's declaring it as a new variable in the scope of that function. You still need to pass it when you call the function. So instead of if( angular.isUndefinedOrNull() ) you want if( angular.isUndefinedOrNull(configId) )
ah it seems we had similar thoughts. Good, all is well now.
angular.isUndefinedOrNull is really a function in AngularJS? What version? I can't see it in API Reference in AngularJS' site
@AlexCoroza Old question, but seems like it's a function iProgrammer defined and not a part of Angular. iProgrammer, please correct me if I'm wrong?
@boi_echos I believe it's just angular.isUndefined now. docs.angularjs.org/api/ng/function/angular.isUndefined
1

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

2 Comments

This is not useful if 0 or false are possible valid values of your variable.
@yms - Agreed with your comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.