简体   繁体   English

$ http请求期间延迟忙/加载指示器

[英]Delay Busy/Loading Indicator During $http Request

I want to implement a very simple Loading directive during $http requests. 我想在$ http请求期间实现一个非常简单的Loading指令。 I want to add a delay, which means that if the promise is returned within a specified time say 1 sec, the indicator is simply not shown to prevent flicker. 我要添加一个延迟,这意味着,如果在指定的时间(例如1秒)内返回了承诺,就不会显示该指标以防止闪烁。 And it shows only if request/promise takes more than 1 sec. 并且仅在请求/承诺花费1秒以上时显示。 I have tried using $timout as: 我尝试使用$ timout作为:

if (_delay) {
    $timeout(function () {
        scope.$root.ShowOverlay = true;
    }, 1000);
}

But it won't help as I know. 但据我所知,这无济于事。 How can i accomplish this? 我怎样才能做到这一点? I have setup this plunk . 我已经安装此普拉克

Since my directive stays out of ngView. 由于我的指令不会出现在ngView中。 I am using $rootScope and controller to update rootScope parameter "OverlayText" . 我正在使用$ rootScope和控制器来更新rootScope参数“ OverlayText” I would highly appreciate any improvements. 我将不胜感激任何改善。

Please don't test syntax :( 请不要测试语法:(

function getData(){
    $scope.data = null;
    $httpCall("url/properties",functionCallback(response){
        $scope.data = response;
        scope.$root.ShowOverlay = false;
        return $scope.data;
    });
    $timeout(function(){
        if($scope.data != null)
        {
            scope.$root.ShowOverlay = true;
        }
    },1000);
}

getData();

Probably this should be in a service, but in the meantime, this is working for me. 可能这应该在服务中,但是与此同时,这对我有用。

myApp.controller('MainCtrl', function($scope, $http, $rootScope, $timeout) {    
    var getData = function() {
    $rootScope.OverlayText = 'Loading';
    var isLoading = true;
    $timeout(function(){
      isLoading = false;
      $rootScope.ShowOverlay = true;
    },1000);
    $http.get('https://api.github.com/users/potherca/repos?per_page=100', {
        cache: false
      })
      .then(function(_resp) {
        //This timeout is only for testing, not always the response take longer than 1 sec.
        $timeout(function(){
          isLoading = false;
          $scope.datajson = _resp.data;
          $rootScope.ShowOverlay = false;
        },2500);
      });
  }();   

});

https://plnkr.co/edit/3bEfka4KQZYoIj0GNgA7?p=preview https://plnkr.co/edit/3bEfka4KQZYoIj0GNgA7?p=preview

I used to work with angular-busy https://github.com/cgross/angular-busy/blob/master/README.md 我曾经使用忙碌的https://github.com/cgross/angular-busy/blob/master/README.md

On your controller, just provide it with the promise returned from the $http call. 在您的控制器上,只需为其提供从$ http调用返回的promise。 You can also set a "min duration" for it, so it will be shown only if the promise wasn't resolve after 1 sec. 您也可以为其设置“最小持续时间”,因此只有在1秒钟后诺言没有解决时才会显示。 You can also configure template for it, to design your own "loader" indication. 您也可以为其配置模板,以设计自己的“加载程序”指示。

I works really nice for us. 我对我们来说真的很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在使用jQuery的AJAX请求期间显示忙碌加载指示器 - Showing Busy loading Indicator during an AJAX Request using jQuery AngularJS加载指示符,带有angular-busy和$ stateChangeStart - AngularJS loading indicator with angular-busy and $stateChangeStart JS忙加载指示器忽略中间点击 - JS Busy loading indicator ignore middle click 加载数据时浏览器忙指示 - Browser busy indicator when loading data 延迟期间加载图像 - loading image during delay Javascript - 加载/忙碌指示器或事件点击页面上的透明 div - Javascript - loading/busy indicator or transparent div over page on event click sapui5 如何为同步 Ajax GET 请求提供忙碌指示器 - sapui5 how to provide Busy Indicator for Synchronous Ajax GET request 如何在加载页面时显示忙碌指示器,并且需要在primefaces,javascript中完成页面加载后隐藏忙碌指示器 - How to show the busy indicator while loading the page, and we need to hide the busy indicator after page load completed in primefaces,javascript React.js - 具有延迟和防闪烁的加载指示器 - React.js - Loading Indicator with a delay and anti-flickering 添加加载指示器或微调器以延迟渲染(不适用于订阅准备) - Add a loading indicator or spinner for rendering delay (Not for subscription ready)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM