Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show/hide if variable is null

Tags:

angularjs

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.

like image 485
Paul Haggo Avatar asked Jan 27 '14 21:01

Paul Haggo


People also ask

How do I hide a div if there is no content?

$("#bar"). find('div. section:empty'). hide();

How do I check if a string is null or empty in typescript?

You can simply use typeof. It will check undefined, null, 0 and "" also.

Can Div be hidden?

The hidden attribute hides the <div> element. You can specify either 'hidden' (without value) or 'hidden="hidden"'. Both are valid. A hidden <div> element is not visible, but it maintains its position on the page.


2 Answers

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

or

<div ng-show="myvar != null"></div> 
like image 144
Gruff Bunny Avatar answered Sep 23 '22 12:09

Gruff Bunny


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; 
like image 28
Paul Haggo Avatar answered Sep 25 '22 12:09

Paul Haggo