2017-03-23 51 views
2

我对AngularJS有一个轻微的理解,但没有更多。我继承了一个Java应用程序,它是标准的Spring MVC和AngularJS的混合体。其中一个AngularJS控制器调用标准的MVC控制器来获取json数据集。当这个控制器花费太长时间时,angularJS页面出错 - 大概是超时。AngularJS控制器执行获取超时

我有一个快速浏览,在我看来这可能是一个设计错误,我应该在控制器中进一步做这个调用,并使用$然后调用,但我想尽可能少做出更改所以我想我会看看是否有一个较小的变化/更好的建议。

的控制器,与胆量撕掉了,是这样的

'use strict' 

var pageController = [ 
    '$scope', 
    '$http', 
    '$location', 
    '$window', 
    '$filter', 

    function($scope, $http, $location, $window, $filter) { 

     $.urlParam = function(name){ 
      ... 
      return val; 
     } 

     $http 
     .get(
      "/dataretrievalendpoint?searchparam=" + $.urlParam("searchparam")) 
     .then(
       function(response) { 
        alert("All is well"); 
        .... 
       }, 
       function(response) { 
        alert("Error occurred !"); 
       } 
       ); 
     } 
    ]; 

所以基本上,如果获得(“/ dataretrievalendpoint ...)花费的时间太长(好像毫秒和我的笔记本电脑相当于大约2000条记录)它会发生“错误发生”,否则“一切正常”。

有没有办法让.get成为未来或延长等待时间?还是我需要调查做某件事沿线

$scope.param.$then = function(){ return make data retrieval call; } 

AngularJS Controller wait for response (or set callback)

或者我应该在做其他事情吗?!

回答

0

您可以使用$httpInterceptors扩展默认的$ http超时。请检查下面的代码。

angular.module('myApp') 
.config([ function() { 

    $httpProvider.interceptors.push('timeoutHttpIntercept'); 

}]) 

.factory('timeoutHttpIntercept', [function() { 
    return { 
     'request': function (config) { 
      config.timeout = 200000; 
      return config; 
     } 
    }; 
}]); 

这里我们做的是我们将默认http超时扩展到20秒。谢谢。

+0

嗨,我们有一个稍微不同的设置。这应该工作吗?道歉会在明天看到我自己的文档,但我想我会问是否显而易见! (app.js)'use strict'var app = angular.module('app',['ngRoute','ngSanitize'])。config([,function(){ \t $ httpProvider.interceptors.push(' timeoutHttpIntercept '); \t}])工厂(' timeoutHttpIntercept”,[函数(){ \t返回{ \t '请求':功能(配置){ \t config.timeout = 200000; \t返回配置; \t} \t}; \t}]); – gringogordo

+0

@gringogordo:我更新了我的代码。请看看它。早些时候,我把一个不需要的“,”我的配置。我已纠正它。请在.config([,function ..)中删除“,”然后重试。是的,它应该可以工作。 –