How to check if a scope variable is undefined in AngularJS template?

AngularjsAngularjs Ng-Show

Angularjs Problem Overview


How to check if a scope variable is undefined?

This does not work:

<p ng-show="foo == undefined">Show this if $scope.foo == undefined</p>

Angularjs Solutions


Solution 1 - Angularjs

Here is the cleanest way to do this:

<p ng-show="{{foo === undefined}}">Show this if $scope.foo === undefined</p>

No need to create a helper function in the controller!

Solution 2 - Angularjs

Using undefined to make a decision is usually a sign of bad design in Javascript. You might consider doing something else.

However, to answer your question: I think the best way of doing so would be adding a helper function.

$scope.isUndefined = function (thing) {
    return (typeof thing === "undefined");
}

and in the template

<div ng-show="isUndefined(foo)"></div>

Solution 3 - Angularjs

Corrected:

HTML

  <p ng-show="getFooUndef(foo)">Show this if $scope.foo === undefined</p>

JS

$scope.foo = undefined;

$scope.getFooUndef = function(foo){
    return ( typeof foo === 'undefined' );
}

Fiddle: http://jsfiddle.net/oakley349/vtcff0w5/1/

Solution 4 - Angularjs

Posting new answer since Angular behavior has changed. Checking equality with undefined now works in angular expressions, at least as of 1.5, as the following code works:

ng-if="foo !== undefined"

When this ng-if evaluates to true, deleting the percentages property off the appropriate scope and calling $digest removes the element from the document, as you would expect.

Solution 5 - Angularjs

If foo is not a boolean variable then this would work (i.e. you want to show this when that variable has some data):

<p ng-show="!foo">Show this if $scope.foo is undefined</p>

And vise-versa:

<p ng-show="foo">Show this if $scope.foo is defined</p>

Solution 6 - Angularjs

If you're using Angular 1, I would recommend using Angular's built-in method:

angular.isDefined(value);

reference : https://docs.angularjs.org/api/ng/function/angular.isDefined

Solution 7 - Angularjs

You can use the double pipe operation to check if the value is undefined the after statement:

<div ng-show="foo || false">
    Show this if foo is defined!
</div>
<div ng-show="boo || true">
    Show this if boo is undefined!
</div>

Check JSFiddle for demo

For technical explanation for the double pipe, I prefer to take a look on this link: https://stackoverflow.com/a/34707750/6225126

Solution 8 - Angularjs

As @impulsgraw wrote. You need to check for undefined after the pipes:

<div ng-show="foo || undefined">
    Show this if foo is defined!
</div>
<div ng-show="boo || !undefined">
    Show this if boo is undefined!
</div>

https://jsfiddle.net/mjfz2q9h/11/

Solution 9 - Angularjs

Solution 10 - Angularjs

<p ng-show="angular.isUndefined(foo)">Show this if $scope.foo === undefined</p>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSebastian BarthView Question on Stackoverflow
Solution 1 - AngularjsjlewkovichView Answer on Stackoverflow
Solution 2 - AngularjsUmur KontacıView Answer on Stackoverflow
Solution 3 - AngularjsOakleyView Answer on Stackoverflow
Solution 4 - AngularjsTahsis ClausView Answer on Stackoverflow
Solution 5 - AngularjsNaguib IhabView Answer on Stackoverflow
Solution 6 - AngularjsSyed UsmanView Answer on Stackoverflow
Solution 7 - AngularjsMahdi FathallaView Answer on Stackoverflow
Solution 8 - AngularjsD-UnitView Answer on Stackoverflow
Solution 9 - Angularjshamil.DevView Answer on Stackoverflow
Solution 10 - AngularjsFalconView Answer on Stackoverflow