Ответ 1
Вы можете использовать массив $http.pendingRequests
для объектов конфигурации для текущих ожидающих запросов. Это можно использовать так:
$scope.isLoading = function () {
return $http.pendingRequests.length !== 0;
};
Я делаю запрос $http
, когда пользователь нажимает кнопку <button>
, и я отключаю/скрываю/показываю несколько элементов на экране, пока запрос не вернется с помощью success
или error
Есть ли способ узнать, что $http
еще не получил ответа? То, как я делаю это прямо сейчас, это у меня есть var в моем контроллере под названием $scope.requesting
, который затем я использую на своей HTML-странице так:
<img src="img/loader.gif" ng-show="requesting" />
так что в основном, когда $scope.requesting
истинно, покажите спиннинг ajaxyish loader.
Я хотел бы по возможности отключить $scope.requesting
и использовать любое $http
, если оно вообще есть.
Контроллер входа
function LoginForm($scope, $http)
{
$scope.requesting = false;
$scope.login = function()
{
$scope.requesting = true;
$http.post('resources/request.php', data, {timeout:20000})
.success(function(data, status, headers, config)
{
$scope.requesting = false;
})
.error(function(data, status, headers, config)
{
$scope.requesting = false;
}
);
}
}
Вы можете использовать массив $http.pendingRequests
для объектов конфигурации для текущих ожидающих запросов. Это можно использовать так:
$scope.isLoading = function () {
return $http.pendingRequests.length !== 0;
};
этот проект jsbin принимает ответ @DmitryEvseev на следующий шаг. Он обеспечивает более точный контроль над тем, какие запросы могут использоваться для запуска "загрузки...".
Те запросы с { showLoader: true }
используются для отображения панели "Загрузка...".
HTML
<div ng-app="app">
<div ng-controller="spinnerController as vm">
<div ng-if="vm.isLoading()">Loading ...</div>
</div>
</div>
Javascript
angular
.module('app', [])
.config(config)
.factory('httpLoader', httpLoader)
.factory('httpLoaderInterceptor', httpLoaderInterceptor)
.controller('spinnerController', spinnerController);
function config($httpProvider) {
//adding the default http status code handler
$httpProvider.interceptors.push('httpLoaderInterceptor');
}
function httpLoader() {
var pendingReqs = {};
var factory = {
addPendingReq: addPendingReq,
subtractPendingReq: subtractPendingReq,
getPendingReqs: getPendingReqs
};
return factory;
function addPendingReq(url) {
console.log('adding url', url);
pendingReqs[url] = true;
}
function subtractPendingReq(url) {
console.log('removing url', url);
delete pendingReqs[url];
}
function getPendingReqs() {
return sizeOf(pendingReqs);
}
}
function httpLoaderInterceptor($q, httpLoader) {
var factory = {
request: request,
response: response,
responseError: responseError
};
return factory;
function request(config) {
console.log('request', config.url);
if (config.showLoader) {
httpLoader.addPendingReq(config.url);
}
return config;
}
function response(res) {
console.log('response', res.config.url);
if (res.config.showLoader) {
httpLoader.subtractPendingReq(res.config.url);
}
}
function responseError(res) {
console.log('responseError', res.config.url);
if (res.config.showLoader) {
httpLoader.subtractPendingReq(res.config.url);
}
return $q.reject(res);
}
}
function spinnerController($http, httpLoader) {
var self = this;
self.isLoading = function() {
return httpLoader.getPendingReqs() > 0;
};
$http.get('http://stackoverflow.com/posts/34561385',{
showLoader: true
});
$http.get('http://www.amazon.com', {
showLoader: true
});
$http.get('http://www.yahoo.com',{
showLoader: true
});
$http.get('http://www.stackoverflow.com',{
showLoader: true
});
}
function sizeOf(obj) {
var size = 0,
key;
for (key in obj) {
if (obj.hasOwnProperty(key)) {
size++;
}
}
return size;
}
Попробуйте эту директиву: https://github.com/afeiship/angular-isloading
body {
font-family: 'STHeiti', 'Microsoft YaHei', Helvetica, Arial, sans-serif;
-webkit-tap-highlight-color: rgba(0, 0, 0, 0)
}
.loading-widget {
width: 100px;
height: 100px;
margin: auto;
top: 0;
bottom: 0;
left: 0;
right: 0;
position: absolute;
}
.loading-widget,
.loading-widget[data-visible] {
display: none;
}
.loading-widget[data-visible=true] {
display: block;
}
.loading-widget img {
width: 100%;
height: 100%;
}
<div class="loading-widget"
isloading
loading="loading"
data-visible="{{loading}}"
>
<img src="svg/default.svg" alt="">
</div>
angular.module('TestApp', ['nx.widget']);
angular.module('TestApp').
controller('MainCtrl', function ($http, $q, $rootScope) {
$rootScope.loading = false;
var s1 = $http.get('http://www.baidu.com');
var s2 = $http.get('http://www.sina.com');
var s3 = $http.get('http://www.163.com');
var s4 = $http.get('http://www.qq.com');
var s5 = $http.get('http://www.hao123.com');
//you need a VPN if you're a Chinese(Thanks to the GFW)
var s6 = $http.get('https://www.google.com/');
$q.all([s1, s2, s3, s4, s5, s6]).then(function (responses) {
console.log(responses);
})
});
<!--1.attach the directive-->
<!--2.write the scope.loading to the app.$rootScope-->
<!--2.read loading props for CSS-->
Ни один из ответов здесь не прибил его для меня, и я избегаю использования $http.pendingRequests
, поэтому вот что я сделал
Мой вариант использования заключался в том, что я должен был показать простое сообщение "Загрузка.." в верхней части моего окна просмотра, если бы у меня были какие-либо запросы на рейс, выполняемые на сервере.
Внутри .config
я зарегистрировал новый Interceptor. и внутри там я добавил простой счетчик, который увеличивается на 1 за каждый новый запрос и уменьшается на каждый ответ.
$httpProvider.interceptors.push([function () {
var pendingRequestsCounter = 0;
return {
request: function (config) {
pendingRequestsCounter++;
if (pendingRequestsCounter > 0) {
// we have some pending requests, so do something here
}
return config;
},
response: function (response) {
pendingRequestsCounter--;
if (pendingRequestsCounter === 0) {
// we have no pending requests, so do something else here
}
return response;
}
};
}]);