更新时间:2022-10-23 18:43:24
您可以使用 $ http.pendingRequests
阵列配置对象为目前正在等待的请求。它是可以用这种方式:
$ scope.isLoading =功能(){
返回$ http.pendingRequests.length == 0!;
};
I make an $http
request when a user clicks a <button>
and I disable/hide/show several elements on screen until the request comes back with either success
or error
Is there a way to know that $http
hasn't had a response yet? The way I am doing it right now is I have a var in my controller called $scope.requesting
which I then use in my HTML page like so:
<img src="img/loader.gif" ng-show="requesting" />
so basically when $scope.requesting
is true, show the spinning ajaxyish loader.
I'd like to ditch the $scope.requesting
if possible and use whatever $http
has to offer, if any at all.
Login Controller
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;
}
);
}
}
You can make use of $http.pendingRequests
array of config objects for currently pending requests. It is possible to use it that way:
$scope.isLoading = function () {
return $http.pendingRequests.length !== 0;
};