如果变量为空,如何显示/隐藏 [英] How to show/hide if variable is null
问题描述
我想根据变量是否为空来显示/隐藏 div.
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.
谢谢.
推荐答案
澄清一下,上面的例子确实有效,我在例子中的代码由于无关的原因而无效.
To clarify, the above example does work, my code in the example did not work for unrelated reasons.
如果 myvar 为 false、null 或以前从未使用过(即从未调用过 $scope.myvar 或 $rootScope.myvar),则 div 将不会显示.一旦为其分配了任何值,div 就会显示,除非该值特别为 false.
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.
以下将导致 div 显示:
$scope.myvar = "Hello World";
或
$scope.myvar = true;
以下将隐藏 div:
$scope.myvar = null;
或
$scope.myvar = false;
这篇关于如果变量为空,如何显示/隐藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!