本文介绍了要知道一个方式,当角$ HTTP是"请求"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做一个 $ HTTP 请求时,用户点击一个<按钮> 和我禁用/隐藏/在屏幕上显示多个元素,直到请求回来了两种成功错误

有没有办法知道 $ HTTP 一直没有回应了吗?我现在正在做它的方式是我在叫控制器中的无功 $ scope.requesting 然后我在我的HTML页面中使用像这样:

 < IMG SRC =IMG / loader.gifNG秀=请求/>

所以基本上,当 $ scope.requesting 为真,显示纺纱ajaxyish装载机。

我想沟 $ scope.requesting 如果可能的话,并使用任何 $ HTTP 所提供的(如果有)都没有。

登录控制器

 函数LoginForm的($范围,$ HTTP)
{
    $ scope.requesting = FALSE;    $ scope.login =功能()
    {
        $ scope.requesting = TRUE;        $ http.post('资源/ request.php',数据{超时:20000})
            .success(功能(数据,状态,头,配置)
            {
                $ scope.requesting = FALSE;
            })
            .error(功能(数据,状态,头,配置)
            {
                $ scope.requesting = FALSE;
            }
        );
    }
}


解决方案

您可以使用 $ 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;
};

这篇关于要知道一个方式,当角$ HTTP是&QUOT;请求&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 04:29