javascript - 一种知道 Angular $http 何时为 "requesting"的方法

标签 javascript angularjs

我做了一个 $http用户点击 <button> 时的请求我禁用/隐藏/显示屏幕上的几个元素,直到请求返回 successerror
有没有办法知道$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/

相关文章:

javascript - 在 Laravel 5 中使用 JQuery 从 href 中提取值时出错

javascript - 如何使用 ajax 调用更新/更改数据库值而不刷新页面

javascript - Angular HTML5 模式和 ui-router 返回 403

javascript - 如果元素是由指令生成的,ng-click 不起作用

javascript - 将子数组添加到 Javascript 中的对象中

javascript - 带有 xeditable 的 Angular js 中的多选下拉列表

javascript - 滚动时触发 Tweenlite/Tweenmax 的连续动画

javascript - 通过 Chrome 扩展查看 onclick 数据

javascript - 带有 jquery Math 和 CSS 百分比值的视差

javascript - AngularJS:ng-if 超出 ng-repeat 会破坏 ng-repeat