2

I have a slight passing understanding of AngularJS but nothing more. I have inherited a java app that is a mix between standard Spring MVC and AngularJS. One of the AngularJS controllers calls a standard MVC controller to get a json data set. When this controller takes too long the angularJS page errors out - presumably a timeout.

I have had a quick look around and it seems to me this is probably a design error and I should do this call further in the controller and use a $then call but I'd like to make as few changes as possible so I thought I'd see if there was a smaller change / better suggestion.

The controller, with the guts ripped out, is like this

'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 !");
                }
               );
        }
    ];

So essentially if the .get("/dataretrievalendpoint...) takes too long (seems like milliseconds and on my laptop equates to about 2000 records) it goes to "Error occurred", otherwise "All is well".

Is there a way I can turn the .get into a future or extend the wait time ? or do I need to investigate doing something along the lines of

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

(AngularJS Controller wait for response (or set callback))

Or should I be doing something else entirely ?!

Community
  • 1
  • 1
gringogordo
  • 1,990
  • 6
  • 24
  • 60

1 Answers1

0

You can use $httpInterceptors to extend the default $http timeout. Please check the below code.

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

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

}])

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

Here what we does is that we extend the default http timeout to 20 seconds. Thanks.

Muhammed Neswine
  • 2,028
  • 1
  • 20
  • 20
  • Hi we have a slightly different set up. Should this work ? Apologies will look at the docs properly myself tomorrow but thought I'd ask if it was obvious ! (app.js) 'use strict' var app=angular.module('app',['ngRoute','ngSanitize']).config([, function() { $httpProvider.interceptors.push('timeoutHttpIntercept'); }]).factory('timeoutHttpIntercept', [function () { return { 'request': function (config) { config.timeout = 200000; return config; } }; }]); – gringogordo Mar 23 '17 at 15:48
  • @gringogordo: i have updated my code. Please take a look at it. Earlier i had put an unwanted "," i config. I have corrected it. Please remove the "," in .config([, function.. and try again. And yes, it should work. – Muhammed Neswine Mar 23 '17 at 16:11