How to show/hide if variable is null

Angularjs

Angularjs Problem Overview


I'm wanting to show/hide a div based on whether a variable is null or not.

<div ng-show="myvar"></div>

Note: the variable in my case is an object.

A very simple question, but I can't seem to make it work.

Thanks.

Angularjs Solutions


Solution 1 - Angularjs

<div ng-hide="myvar == null"></div>

or

<div ng-show="myvar != null"></div>

Solution 2 - Angularjs

To clarify, the above example does work, my code in the example did not work for unrelated reasons.

If myvar is false, null or has never been used before (i.e. $scope.myvar or $rootScope.myvar never called), the div will not show. Once any value has been assigned to it, the div will show, except if the value is specifically false.

The following will cause the div to show:

$scope.myvar = "Hello World";

or

$scope.myvar = true;

The following will hide the div:

$scope.myvar = null;

or

$scope.myvar = false;

Solution 3 - Angularjs

In this case, myvar should be a boolean value. If this variable is true, it will show the div, if it's false.. It will hide.

Check this out.

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
QuestionPaul HaggoView Question on Stackoverflow
Solution 1 - AngularjsGruff BunnyView Answer on Stackoverflow
Solution 2 - AngularjsPaul HaggoView Answer on Stackoverflow
Solution 3 - AngularjsRodrigo OliveiraView Answer on Stackoverflow