2017-03-23 53 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 ..)中刪除「,」然後重試。是的,它應該可以工作。 –