小编Ste*_*n M的帖子

全局错误处理程序,仅捕获"未处理"的承诺

我有一个全局错误处理程序,我的角度应用程序写成$http interceptor,但我想更进一步.我想要的是每次$http失败的电话(被拒绝),承诺的任何"链式"消费者应首先尝试解决错误,如果它仍然未解决(未被捕获),那么我想要全球错误处理程序接管.

用例是,我的全局错误处理程序alert box在屏幕顶部显示一个咆哮.但是我弹出了几个模态,我在那里显式处理错误,在模态本身显示错误信息.因此,基本上,这个模态控制器应该将被拒绝的承诺标记为"已处理".但由于拦截器似乎总是第一个运行$http error,我无法想出办法.

这是我的拦截器代码:

angular.module("globalErrors", ['angular-growl', 'ngAnimate'])
    .factory("myHttpInterceptor", ['$q', '$log', '$location', '$rootScope', 'growl', 'growlMessages',
        function ($q, $log, $location, $rootScope, growl, growlMessages) {
            var numLoading = 0;
            return {
                request: function (config) {
                    if (config.showLoader !== false) {
                        numLoading++;
                        $rootScope.loading = true;
                    }
                    return config || $q.when(config)
                },
                response: function (response) {
                    if (response.config.showLoader !== false) {
                        numLoading--;
                        $rootScope.loading = numLoading > 0;
                    }
                    if(growlMessages.getAllMessages().length) { // clear messages on next …
Run Code Online (Sandbox Code Playgroud)

javascript angularjs angular-promise

9
推荐指数
1
解决办法
1149
查看次数

标签 统计

angular-promise ×1

angularjs ×1

javascript ×1