Ron*_*nie 19 javascript angularjs
我$http在用户点击a时发出请求,<button>并在屏幕上禁用/隐藏/显示多个元素,直到请求返回任一success或error
有没有办法知道$http还没有回复?我现在正在做的方式是我的控制器中有一个var,$scope.requesting然后我在我的HTML页面中使用它,如下所示:
<img src="img/loader.gif" ng-show="requesting" />
Run Code Online (Sandbox Code Playgroud)
所以基本上$scope.requesting是真的,显示旋转的ajaxyish装载机.
$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;
}
);
}
}
Run Code Online (Sandbox Code Playgroud)
Dmi*_*eev 32
您可以$http.pendingRequests为当前挂起的请求使用配置对象数组.可以这样使用它:
$scope.isLoading = function () {
return $http.pendingRequests.length !== 0;
};
Run Code Online (Sandbox Code Playgroud)