最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Hide div if value is undefined or blank (Angularjs) - Stack Overflow

programmeradmin2浏览0评论

I have the following. If i do a console.log($scope.variable), i get 2 values of it at different times on the console.

It shows undefined and '' at the console.

I want to hide a div on basis of this value. I do the following

<div ng-hide="$scope.variable== undefined || $scope.variable = ''>
  Hide this section
</div>

It should be able to hide the section since the values are either undefined and '' at all cases. However the div still shows at the UI. Am i missing any constraint or not putting the condition check properly ?

I have the following. If i do a console.log($scope.variable), i get 2 values of it at different times on the console.

It shows undefined and '' at the console.

I want to hide a div on basis of this value. I do the following

<div ng-hide="$scope.variable== undefined || $scope.variable = ''>
  Hide this section
</div>

It should be able to hide the section since the values are either undefined and '' at all cases. However the div still shows at the UI. Am i missing any constraint or not putting the condition check properly ?

Share Improve this question asked Mar 30, 2017 at 18:57 RihanaRihana 4432 gold badges7 silver badges14 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4
<div ng-show="variable != null"></div>

or

<div ng-hide="variable == null>
  Hide this section
</div>

You could use ng-if in this case, which would result in better performance,

 <div ng-if="variable == null">
      Hide this section
 </div>

Ng-show should never be used.It will load view in value and just hides it by setting css property display:none. Instead use ng-if

<div ng-if="variable != null"></div>
or

<div ng-if="variable === null>
  Hide this section
</div>
发布评论

评论列表(0)

  1. 暂无评论