Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.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 如何在所有$http调用中设置$http.error()?_Javascript_Angularjs_Service_Error Handling - Fatal编程技术网

Javascript 如何在所有$http调用中设置$http.error()?

Javascript 如何在所有$http调用中设置$http.error()?,javascript,angularjs,service,error-handling,Javascript,Angularjs,Service,Error Handling,我将如何为$http服务中的所有.error调用(无论在何处使用)设置函数 在我的整个应用程序中,我使用了许多封装在服务中的$http调用,并希望避免重复实现所有.error方法 e、 g 您可以创建和侦听器来捕获所有错误 像这样的 var applicationWideInterceptors = function ($q, toastrSrvc) { return { responseError:function(rejection) { //

我将如何为$http服务中的所有.error调用(无论在何处使用)设置函数

在我的整个应用程序中,我使用了许多封装在服务中的$http调用,并希望避免重复实现所有.error方法

e、 g


您可以创建和侦听器来捕获所有错误

像这样的

var applicationWideInterceptors = function ($q, toastrSrvc) {
    return {
        responseError:function(rejection) {
            //do something with the rejection
            if (rejection.status === 404) {
                toastrSrvc.error(rejection.data);
            }
            return $q.reject(rejection);
        }
    }
};

app.factory("appWideIntercpt", ["$q", "toastrSrvc", applicationWideInterceptors]);
别忘了将其添加到配置块中

$httpProvider.interceptors.push("appWideIntercpt"

一种可能的方法是设置自己的方法。
$httpProvider.interceptors.push("appWideIntercpt"