[英]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.