如何显示/隐藏变量为空

我想根据变量是否为 null 来显示/隐藏 div。

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

注意: 在我的例子中,变量是一个对象。

一个很简单的问题,但我似乎不能让它工作。

谢谢。

178320 次浏览

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.

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

or

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

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;