我做了一个 $http
用户点击 <button>
时的请求我禁用/隐藏/显示屏幕上的几个元素,直到请求返回 success
或 error
有没有办法知道$http
还没有回应?我现在的做法是我的 Controller 中有一个名为 $scope.requesting
的变量。然后我在我的 HTML 页面中使用它,如下所示:
<img src="img/loader.gif" ng-show="requesting" />
所以基本上当
$scope.requesting
是的,显示旋转的 ajaxyish 加载器。我想放弃
$scope.requesting
如果可能,使用任何 $http
必须提供,如果有的话。登录 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;
}
);
}
}
最佳答案
您可以使用$http.pendingRequests
当前待处理请求的配置对象数组。可以这样使用它:
$scope.isLoading = function () {
return $http.pendingRequests.length !== 0;
};
关于javascript - 一种知道 Angular $http 何时为 "requesting"的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15529496/