The message for empty list for ng-repeat always flash?

情到浓时终转凉″ 提交于 2020-01-06 17:59:55

问题


The following code from an ionic project supposes to display message "No record" when the events is empty. However, the message always show for half a second and then disappear even when events is not empty? How to get rid of the flash?

<a ng-repeat="event in events">
  ....
</a>
<div ng-hide="events.length"> <!-- <div ng-show="!events.length"> behaves the same -->
  <div ....>
  No record.
  </div>
<div>

Update: A testable plunker is created https://plnkr.co/edit/lchTKN6C8niAsALquHoF?p=preview, you can see the red message still flash even the list is not empty?


回答1:


Try ng-cloak along with ng-hide/ng-show

<div ng-hide="events.length" ng-cloak> <!-- <div ng-show="!events.length"> behaves the same -->
  No record.
<div>

The ngCloak directive is used to prevent the Angular html template from being briefly displayed by the browser in its raw (uncompiled) form while your application is loading. Use this directive to avoid the undesirable flicker effect caused by the html template display.

Documentation for ng-cloak

UPDATE:

Since the $scope.events array got initialized with an empty value before the return of the service completes. The convention is to use the $promise object to get access to the result of the deferred task when it completes.

var allEvents = EventService.query({ category: "Music" });

allEvents.$promise.then(function (response) {
    $scope.events = response;
});

Demo of working plunk



来源:https://stackoverflow.com/questions/37506812/the-message-for-empty-list-for-ng-repeat-always-flash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!