How to set a global http timeout in AngularJs

前端 未结 4 1591
予麋鹿
予麋鹿 2020-11-27 02:23

I know I can set a timeout each and every time:

$http.get(\'path/to/service\', {timeout: 5000});

... but I want to set a global timeout to

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-27 03:13

    This is possible with bleeding-edge angular.js (tested with git master 4ae46814ff).

    You can use request http interceptor. Like this.

     angular.module('yourapp')
      .factory('timeoutHttpIntercept', function ($rootScope, $q) {
        return {
          'request': function(config) {
            config.timeout = 10000;
            return config;
          }
        };
     });
    

    And then in .config inject $httpProvider and do this:

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

提交回复
热议问题