简体   繁体   中英

The ng-hide element appears for a while and then hide?

<div class="alert alert-warning" ng-hide="badges.length">      
    <strong>Empty!</strong> This Micro_organization has No badges.
</div>
<ul> 
  <li ng-repeat="badge in badges">{{ badge.title }}</li>
</ul>

even if badges are not empty, the message Empty! This Micro_organization has No badges. Empty! This Micro_organization has No badges. appears for a while.

Is there any solution so that the message will only show if badges is empty?

if you are using ajax request to get the data then badges will undefined untill ajax call return the data.

untill the data receive badges.length will stay undefined then then ng-hide="badges.length" will result in something ng-hide="false" thats may be the case of partially apearing the div

--solution--

if you declare $scope.badge before u get the resource data remove that declaration. declare $scope.badge only after the resource call. and use

 <div class="alert alert-warning" ng-hide="badges.length != 0">..

Plunker

Before badges is loaded, the variable is undefined. You can work around this problem by changing the ng-hide expression to ensure badges exists.

ng-hide="!badges || badges.length"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM