繁体   English   中英

angularjs:Promise或http.get(…).then()中的异步计量器

[英]angularjs: async metter in Promise or http.get(…).then()

我使用$ http.get从端点获取大约2MB的数据集。 下面是我的示例代码:

$scope.getData = function (){
    return new Promise(function(resolve, reject) {
            $http({
                method: 'GET',
                url: apiUrlEndPointHost+'/rest/getAll',
            })
            .then(function(response) {
                if(response.data.code<0){
                    reject(response);
                }
                else{
                    resolve(response);
                }
            },function(err) {
                reject(err); 
            });
    });
}

$scope.getData().then(function(response){
   $scope.myData=response.data;
});

获取数据大约需要10秒钟,这一次,我的网站变得如此缓慢,我无法通过事件单击按钮来执行操作。

请帮忙。

您不需要返回新的承诺。 $http已经返回了一个承诺。 您可以在“文档”中看到。 https://docs.angularjs.org/api/ng/service/$http

只需使用此:

$scope.getData = function () {
  return $http({
    method: 'GET',
    url: apiUrlEndPointHost+'/rest/getAll',
  })
}

$scope.getData().then(function(response){
   $scope.myData=response.data;
});

但这不是问题的根本原因。 在其他地方发行。 也许请求挂起这么多时间和UI被冻结。

您可以在“网络开发工具”中跟踪花费了多少时间进行请求。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM