Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何为不同的API端点创建不同的拦截器?_Javascript_Angularjs_Interceptor - Fatal编程技术网

Javascript 如何为不同的API端点创建不同的拦截器?

Javascript 如何为不同的API端点创建不同的拦截器?,javascript,angularjs,interceptor,Javascript,Angularjs,Interceptor,我在AngularJS中有两个服务,对于两个不同的API端点,它们基于$http服务(或者只是$http的包装) 现在我的任务是为每个服务创建错误处理。什么是最好的方法来做这件事呢 伪代码: angular.service('api1', function($http){ ... }); angular.service('api2', function($http){ ... }); 2种解决方案: 只需创建ErrorHandling服务并将其注入两个服务中 要对$http使用拦截器并处理所

我在AngularJS中有两个服务,对于两个不同的API端点,它们基于$http服务(或者只是$http的包装)

现在我的任务是为每个服务创建错误处理。什么是最好的方法来做这件事呢

伪代码:

angular.service('api1', function($http){ ... });
angular.service('api2', function($http){ ... });
2种解决方案:

  • 只需创建ErrorHandling服务并将其注入两个服务中
  • 要对$http使用拦截器并处理所有请求,请执行以下操作:

  • 您希望如何处理错误以及什么类型的错误?所有将要返回http错误回调的错误
     angular.service('api1', function($http, ErrorHandlingService){ ... });
     angular.service('api2', function($http, ErrorHandlingService){ ... });
    
    $httpProvider.interceptors.push(ErrorHandlingService);