Angularjs在ajax请求上加载屏幕

时间:2013-06-17 09:20:33

标签: angularjs angularjs-directive

使用Angularjs,我需要显示一个加载屏幕(一个简单的微调器),直到ajax请求完成。请使用代码段建议任何想法。

15 个答案:

答案 0 :(得分:206)

不是设置范围变量来指示数据加载状态,而是让指令为您完成所有操作:

angular.module('directive.loading', [])

    .directive('loading',   ['$http' ,function ($http)
    {
        return {
            restrict: 'A',
            link: function (scope, elm, attrs)
            {
                scope.isLoading = function () {
                    return $http.pendingRequests.length > 0;
                };

                scope.$watch(scope.isLoading, function (v)
                {
                    if(v){
                        elm.show();
                    }else{
                        elm.hide();
                    }
                });
            }
        };

    }]);

使用此指令,您需要做的就是给任何加载动画元素一个“加载”#39;属性:

<div class="loading-spiner-holder" data-loading ><div class="loading-spiner"><img src="..." /></div></div>

页面上可以有多个加载微调器。在哪里以及如何布置这些旋转器取决于您,指令将自动为您打开/关闭它。

答案 1 :(得分:54)

这是一个例子。它使用简单的ng-show方法和bool。

<强> HTML

<div ng-show="loading" class="loading"><img src="...">LOADING...</div>
<div ng-repeat="car in cars">
  <li>{{car.name}}</li>
</div>
<button ng-click="clickMe()" class="btn btn-primary">CLICK ME</button>

<强> ANGULARJS

  $scope.clickMe = function() {
    $scope.loading = true;
    $http.get('test.json')
      .success(function(data) {
        $scope.cars = data[0].cars;
        $scope.loading = false;
    });
  }

当然你可以将加载框html代码移动到一个指令中,然后在$ scope.loading上使用$ watch。在这种情况下:

<强> HTML

<loading></loading>

ANGULARJS DIRECTIVE

  .directive('loading', function () {
      return {
        restrict: 'E',
        replace:true,
        template: '<div class="loading"><img src="..."/>LOADING...</div>',
        link: function (scope, element, attr) {
              scope.$watch('loading', function (val) {
                  if (val)
                      $(element).show();
                  else
                      $(element).hide();
              });
        }
      }
  })

PLUNK http://plnkr.co/edit/AI1z21?p=preview

答案 2 :(得分:20)

我使用ngProgress

添加&#39; ngProgress&#39;一旦您在HTML中包含脚本/ css文件,就可以使用依赖项。一旦你这样做,你可以设置这样的东西,这将在检测到路线变化时触发。

angular.module('app').run(function($rootScope, ngProgress) {
  $rootScope.$on('$routeChangeStart', function(ev,data) {
    ngProgress.start();
  });
  $rootScope.$on('$routeChangeSuccess', function(ev,data) {
    ngProgress.complete();
  });
});

对于AJAX请求,您可以执行以下操作:

$scope.getLatest = function () {
    ngProgress.start();

    $http.get('/latest-goodies')
         .success(function(data,status) {
             $scope.latest = data;
             ngProgress.complete();
         })
         .error(function(data,status) {
             ngProgress.complete();
         });
};

请记住添加&#39; ngProgress&#39;在执行此操作之前,请与控制器相关。如果您正在执行多个AJAX请求,请在主应用范围内使用增量变量来跟踪您的AJAX请求何时完成,然后再调用&#39; ngProgress.complete();&#39;。

答案 3 :(得分:13)

使用pendingRequests不正确,因为如Angular文档中所述,此属性主要用于调试目的。

我建议使用拦截器来了解是否存在任何活动的异步调用。

module.config(['$httpProvider', function ($httpProvider) {
    $httpProvider.interceptors.push(function ($q, $rootScope) {
        if ($rootScope.activeCalls == undefined) {
            $rootScope.activeCalls = 0;
        }

        return {
            request: function (config) {
                $rootScope.activeCalls += 1;
                return config;
            },
            requestError: function (rejection) {
                $rootScope.activeCalls -= 1;
                return rejection;
            },
            response: function (response) {
                $rootScope.activeCalls -= 1;
                return response;
            },
            responseError: function (rejection) {
                $rootScope.activeCalls -= 1;
                return rejection;
            }
        };
    });
}]);

然后通过$ watch检查指令中的activeCalls是否为零。

module.directive('loadingSpinner', function ($http) {
    return {
        restrict: 'A',
        replace: true,
        template: '<div class="loader unixloader" data-initialize="loader" data-delay="500"></div>',
        link: function (scope, element, attrs) {

            scope.$watch('activeCalls', function (newVal, oldVal) {
                if (newVal == 0) {
                    $(element).hide();
                }
                else {
                    $(element).show();
                }
            });
        }
    };
});

答案 4 :(得分:6)

执行此操作的最佳方法是使用响应拦截器以及自定义指令。使用 $ rootScope。$ broadcast &amp; amp;&amp; $ rootScope。$ on 方法。

由于整个过程记录在一篇写得很好的blog article中,我不会再在此重复。请参阅该文章以了解您所需的实施方案。

答案 5 :(得分:3)

参考这个答案

https://stackoverflow.com/a/17144634/4146239

对我来说是最好的解决方案,但有一种方法可以避免使用jQuery。

.directive('loading', function () {
      return {
        restrict: 'E',
        replace:true,
        template: '<div class="loading"><img src="http://www.nasa.gov/multimedia/videogallery/ajax-loader.gif" width="20" height="20" />LOADING...</div>',
        link: function (scope, element, attr) {
              scope.$watch('loading', function (val) {
                  if (val)
                      scope.loadingStatus = 'true';
                  else
                      scope.loadingStatus = 'false';
              });
        }
      }
  })

  .controller('myController', function($scope, $http) {
      $scope.cars = [];
      
      $scope.clickMe = function() {
        scope.loadingStatus = 'true'
        $http.get('test.json')
          .success(function(data) {
            $scope.cars = data[0].cars;
            $scope.loadingStatus = 'false';
        });
      }
      
  });
<body ng-app="myApp" ng-controller="myController" ng-init="loadingStatus='true'">
        <loading ng-show="loadingStatus" ></loading>
  
        <div ng-repeat="car in cars">
          <li>{{car.name}}</li>
        </div>
        <button ng-click="clickMe()" class="btn btn-primary">CLICK ME</button>
  
</body>

你需要替换$(element).show();和(元素).show(); with $ scope.loadingStatus ='true';和$ scope.loadingStatus ='false';

然后,您需要使用此变量来设置元素的ng-show属性。

答案 6 :(得分:3)

打字稿和角度实施

<强>指令

((): void=> {
    "use strict";
    angular.module("app").directive("busyindicator", busyIndicator);
    function busyIndicator($http:ng.IHttpService): ng.IDirective {
        var directive = <ng.IDirective>{
            restrict: "A",
            link(scope: Scope.IBusyIndicatorScope) {
                scope.anyRequestInProgress = () => ($http.pendingRequests.length > 0);
                scope.$watch(scope.anyRequestInProgress, x => {            
                    if (x) {
                        scope.canShow = true;
                    } else {
                        scope.canShow = false;
                    }
                });
            }
        };
        return directive;
    }
})();

<强>范围

   module App.Scope {
        export interface IBusyIndicatorScope extends angular.IScope {
            anyRequestInProgress: any;
            canShow: boolean;
        }
    }  

<强>模板

<div id="activityspinner" ng-show="canShow" class="show" data-busyindicator>
</div>

CSS
#activityspinner
{
    display : none;
}
#activityspinner.show {
    display : block;
    position : fixed;
    z-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YtqFo82P2cXlTWXQReOiJE5bFqHj4qiUhmBgoSFho59rrKztLVMBQY1BgWzBWe8UUsiuYIGTpMglSaYIcpfnSHEPMYzyB8HZwdrqSMHxAbath2MsqO0zLLorua05OLvJxEAIfkECQoAAAAsAAAAADYANwAABMwQyEmrvTjrzbv/YCiOZGmeaKqurDAMAlsKRE3EsjjYxJDrPN8PRLPhfohELYHQuGBDgIJXU0Q5CKqtOXsdP0otITHjfTtiW2lnE37StXUwFNaSScXaGZvm4r0jU1RWV1hhTIWJiouMjVcFBjUGBY4WBWw1A5RDT3sTkVQGnGYYaUOYPaVip3MXoDyiP3k3GAeoAwdRnRoHoAa5lcHCw8TFxscduyjKIrOeRKRAbSe3I9Um1yHOJ9sjzCbfyInhwt3E2cPo5dHF5OLvJREAOwAAAAAAAAAAAA==') 
    -ms-opacity : 0.4;
    opacity : 0.4;
    background-repeat : no-repeat;
    background-position : center;
    left : 0;
    bottom : 0;
    right : 0;
    top : 0;
}

答案 7 :(得分:2)

还有一个很好的演示,展示了如何在项目中使用Angularjs动画。
此处的链接为http://yearofmoo-articles.github.io/angularjs-animation-article/app/#/ng-repeat (请参阅左上角)
这是一个开源的。这是下载的链接
https://github.com/yearofmoo-articles/AngularJS-Animation-Article
这是教程的链接;
http://www.yearofmoo.com/2013/04/animation-in-angularjs.html
我的观点是,继续下载源文件,然后看看他们是如何实现微调器的。他们可能使用了更好的方法。所以,结帐这个项目。

答案 8 :(得分:2)

如果您使用的是Restangular(非常棒),您可以在api调用期间创建动画。这是我的解决方案。添加响应拦截器和发出rootcope广播的请求拦截器。然后创建一个指令来监听该响应和请求。:

         angular.module('mean.system')
  .factory('myRestangular',['Restangular','$rootScope', function(Restangular,$rootScope) {
    return Restangular.withConfig(function(RestangularConfigurer) {
      RestangularConfigurer.setBaseUrl('http://localhost:3000/api');
      RestangularConfigurer.addResponseInterceptor(function(data, operation, what, url, response, deferred) {
        var extractedData;
        // .. to look for getList operations
        if (operation === 'getList') {
          // .. and handle the data and meta data
          extractedData = data.data;
          extractedData.meta = data.meta;
        } else {
          extractedData = data.data;
        }
        $rootScope.$broadcast('apiResponse');
        return extractedData;
      });
      RestangularConfigurer.setRequestInterceptor(function (elem, operation) {
        if (operation === 'remove') {
          return null;
        }
        return (elem && angular.isObject(elem.data)) ? elem : {data: elem};
      });
      RestangularConfigurer.setRestangularFields({
        id: '_id'
      });
      RestangularConfigurer.addRequestInterceptor(function(element, operation, what, url) {
        $rootScope.$broadcast('apiRequest');
        return element;
      });
    });
  }]);

这是指令:

        angular.module('mean.system')
  .directive('smartLoadingIndicator', function($rootScope) {
    return {
      restrict: 'AE',
      template: '<div ng-show="isAPICalling"><p><i class="fa fa-gear fa-4x fa-spin"></i>&nbsp;Loading</p></div>',
      replace: true,
      link: function(scope, elem, attrs) {
        scope.isAPICalling = false;

        $rootScope.$on('apiRequest', function() {
          scope.isAPICalling = true;
        });
        $rootScope.$on('apiResponse', function() {
          scope.isAPICalling = false;
        });
      }
    };
  })
;

答案 9 :(得分:2)

将此内容包含在“app.config”中:

 $httpProvider.interceptors.push('myHttpInterceptor');

并添加此代码:

app.factory('myHttpInterceptor', function ($q, $window,$rootScope) {
    $rootScope.ActiveAjaxConectionsWithouthNotifications = 0;
    var checker = function(parameters,status){
            //YOU CAN USE parameters.url TO IGNORE SOME URL
            if(status == "request"){
                $rootScope.ActiveAjaxConectionsWithouthNotifications+=1;
                $('#loading_view').show();
            }
            if(status == "response"){
                $rootScope.ActiveAjaxConectionsWithouthNotifications-=1;

            }
            if($rootScope.ActiveAjaxConectionsWithouthNotifications<=0){
                $rootScope.ActiveAjaxConectionsWithouthNotifications=0;
                $('#loading_view').hide();

            }


    };
return {
    'request': function(config) {
        checker(config,"request");
        return config;
    },
   'requestError': function(rejection) {
       checker(rejection.config,"request");
      return $q.reject(rejection);
    },
    'response': function(response) {
         checker(response.config,"response");
      return response;
    },
   'responseError': function(rejection) {
        checker(rejection.config,"response");
      return $q.reject(rejection);
    }
  };
});

答案 10 :(得分:2)

使用angular-busy

为你的app / module添加cgBusy:

angular.module('your_app', ['cgBusy']);

将您的承诺添加到scope

function MyCtrl($http, User) {
  //using $http
  this.isBusy = $http.get('...');
  //if you have a User class based on $resource
  this.isBusy = User.$save();
}

在你的html模板中:

<div cg-busy="$ctrl.isBusy"></div>

答案 11 :(得分:1)

这里是简单的拦截器示例,我在ajax启动时将鼠标设置为等待,并在ajax结束时将其设置为auto。

$httpProvider.interceptors.push(function($document) {
return {
 'request': function(config) {
     // here ajax start
     // here we can for example add some class or show somethin
     $document.find("body").css("cursor","wait");

     return config;
  },

  'response': function(response) {
     // here ajax ends
     //here we should remove classes added on request start

     $document.find("body").css("cursor","auto");

     return response;
  }
};
});

必须在应用程序配置app.config中添加代码。我展示了如何在加载状态下更改鼠标,但在那里可以显示/隐藏任何加载器内容,或者添加,删除一些显示加载器的css类。

拦截器将在每个ajax调用上运行,因此不需要在每次http调用时创建特殊的布尔变量($ scope.loading = true / false等)。

拦截器使用角度 jqLit​​e https://docs.angularjs.org/api/ng/function/angular.element构建,因此不需要Jquery。

答案 12 :(得分:1)

使用show和size属性创建一个指令(您还可以添加更多)

    app.directive('loader',function(){
    return {
    restrict:'EA',
    scope:{
        show : '@',
      size : '@'
    },
    template : '<div class="loader-container"><div class="loader" ng-if="show" ng-class="size"></div></div>'
  }
})

并在html中用作

 <loader show="{{loader1}}" size="sm"></loader>

show 变量中,当任何承诺正在运行时传递 true ,并在请求完成时将其设为 false 。 有效演示 - Angular Loader directive example demo in JsFiddle

答案 13 :(得分:0)

我建立在@DavidLin的回答上,以简化它 - 删除指令中对jQuery的任何依赖。我可以确认这是有效的,因为我在生产应用程序中使用它

function AjaxLoadingOverlay($http) {

    return {
        restrict: 'A',
        link: function ($scope, $element, $attributes) {

            $scope.loadingOverlay = false;

            $scope.isLoading = function () {
                return $http.pendingRequests.length > 0;
            };

            $scope.$watch($scope.isLoading, function (isLoading) {
                $scope.loadingOverlay = isLoading;
            });
        }
    };
}   

我使用ng-show代替jQuery调用来隐藏/显示<div>

我在<div>标记下面放置了<body>

<div ajax-loading-overlay class="loading-overlay" ng-show="loadingOverlay">
    <img src="Resources/Images/LoadingAnimation.gif" />
</div>

这里提供覆盖的CSS可以在进行$ http调用时阻止用户界面:

.loading-overlay {
    position: fixed;
    z-index: 999;
    height: 2em;
    width: 2em;
    overflow: show;
    margin: auto;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
}

.loading-overlay:before {
    content: '';
    display: block;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0,0,0,0.3);
}

/* :not(:required) hides these rules from IE9 and below */
.loading-overlay:not(:required) {
    font: 0/0 a;
    color: transparent;
    text-shadow: none;
    background-color: transparent;
    border: 0;
}

CSS功劳归功于@Steve Seeger&#39; s - 他的帖子:https://stackoverflow.com/a/35470281/335545

答案 14 :(得分:0)

您可以添加条件,然后通过rootscope更改它。在你的ajax请求之前,你只需要调用$ rootScope。$ emit(&#39; stopLoader&#39;);

angular.module('directive.loading', [])
        .directive('loading',   ['$http', '$rootScope',function ($http, $rootScope)
        {
            return {
                restrict: 'A',
                link: function (scope, elm, attrs)
                {
                    scope.isNoLoadingForced = false;
                    scope.isLoading = function () {
                        return $http.pendingRequests.length > 0 && scope.isNoLoadingForced;
                    };

                    $rootScope.$on('stopLoader', function(){
                        scope.isNoLoadingForced = true;
                    })

                    scope.$watch(scope.isLoading, function (v)
                    {
                        if(v){
                            elm.show();
                        }else{
                            elm.hide();
                        }
                    });
                }
            };

        }]);

这绝不是最佳解决方案,但它仍然有效。