Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.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
Angularjs 基于承诺的角度误差处理_Angularjs_Exception Handling_Promise - Fatal编程技术网

Angularjs 基于承诺的角度误差处理

Angularjs 基于承诺的角度误差处理,angularjs,exception-handling,promise,Angularjs,Exception Handling,Promise,我是新的角度和承诺的一般想法,所以请容忍我在这一个 我有以下承诺的一般设置: ApiService.getDataUriForUrl(imageUrl).then(function(url){ requests--; }, function(error){ console.log('oh no!'); }); 我写这篇文章的想法是,函数(error)将捕获在getDataUriForUrl()调用上抛出的异常,但现在我相信它是用来处理then(…区域中的错误的 我应该如何格式化

我是新的角度和承诺的一般想法,所以请容忍我在这一个

我有以下承诺的一般设置:

ApiService.getDataUriForUrl(imageUrl).then(function(url){
    requests--;
}, function(error){
    console.log('oh no!');
});
我写这篇文章的想法是,
函数(error)
将捕获在
getDataUriForUrl()调用上抛出的异常,但现在我相信它是用来处理
then(…
区域中的错误的


我应该如何格式化它以截获所需的错误?(捕获getDataUriForUrl()调用中抛出的异常)。

onFulfill回调中抛出的错误不会传递给onReject回调。 相反,您可以将第一个回调传递给then方法,并在返回时调用then方法

ApiService.getDataUriForUrl(imageUrl).then(function(url){
    requests--;
    return new Error ();// you can't throw it
}).then(null, function(error){
    console.log('oh no!');// I can catch error from the getData or the onFulfill callback
});

onFulfill回调中抛出的错误不会传递给onReject回调。 相反,您可以将第一个回调传递给then方法,并在返回时调用then方法

ApiService.getDataUriForUrl(imageUrl).then(function(url){
    requests--;
    return new Error ();// you can't throw it
}).then(null, function(error){
    console.log('oh no!');// I can catch error from the getData or the onFulfill callback
});

onFulfill回调中抛出的错误不会传递给onReject回调。 相反,您可以将第一个回调传递给then方法,并在返回时调用then方法

ApiService.getDataUriForUrl(imageUrl).then(function(url){
    requests--;
    return new Error ();// you can't throw it
}).then(null, function(error){
    console.log('oh no!');// I can catch error from the getData or the onFulfill callback
});

onFulfill回调中抛出的错误不会传递给onReject回调。 相反,您可以将第一个回调传递给then方法,并在返回时调用then方法

ApiService.getDataUriForUrl(imageUrl).then(function(url){
    requests--;
    return new Error ();// you can't throw it
}).then(null, function(error){
    console.log('oh no!');// I can catch error from the getData or the onFulfill callback
});

不必为每个api方法调用$q.reject或$q.resolve,您只需为response和responseError.编写一个拦截器,其中您只需$q.reject()或$q.resolve()。 这将更加干净和模块化

示例代码:

(function (global) {
    "use strict";

    angular.module("TestAPp").factory('httpInterceptor', ["$rootScope", "$q", function ($rootScope, $q) {
        return {
            response:function(response){
                return response;
            },
            responseError: function (response) {
                return $q.reject(response);
            }
        };
    }]);

}(this));
//注册转发器

(function (global) {
    "use strict";

    angular.module("TestApp", [])
           .config([ '$httpProvider',function ($httpProvider) {
                /* Register the interceptor */
                $httpProvider.interceptors.push('httpInterceptor');

    }]);

}(this));

有关更多信息,您可以参考。

对于每个api方法,您只需为response和responseError编写一个拦截器,而不是调用$q.reject()或$q.resolve()。 这将更加干净和模块化

示例代码:

(function (global) {
    "use strict";

    angular.module("TestAPp").factory('httpInterceptor', ["$rootScope", "$q", function ($rootScope, $q) {
        return {
            response:function(response){
                return response;
            },
            responseError: function (response) {
                return $q.reject(response);
            }
        };
    }]);

}(this));
//注册转发器

(function (global) {
    "use strict";

    angular.module("TestApp", [])
           .config([ '$httpProvider',function ($httpProvider) {
                /* Register the interceptor */
                $httpProvider.interceptors.push('httpInterceptor');

    }]);

}(this));

有关更多信息,您可以参考。

对于每个api方法,您只需为response和responseError编写一个拦截器,而不是调用$q.reject()或$q.resolve()。 这将更加干净和模块化

示例代码:

(function (global) {
    "use strict";

    angular.module("TestAPp").factory('httpInterceptor', ["$rootScope", "$q", function ($rootScope, $q) {
        return {
            response:function(response){
                return response;
            },
            responseError: function (response) {
                return $q.reject(response);
            }
        };
    }]);

}(this));
//注册转发器

(function (global) {
    "use strict";

    angular.module("TestApp", [])
           .config([ '$httpProvider',function ($httpProvider) {
                /* Register the interceptor */
                $httpProvider.interceptors.push('httpInterceptor');

    }]);

}(this));

有关更多信息,您可以参考。

对于每个api方法,您只需为response和responseError编写一个拦截器,而不是调用$q.reject()或$q.resolve()。 这将更加干净和模块化

示例代码:

(function (global) {
    "use strict";

    angular.module("TestAPp").factory('httpInterceptor', ["$rootScope", "$q", function ($rootScope, $q) {
        return {
            response:function(response){
                return response;
            },
            responseError: function (response) {
                return $q.reject(response);
            }
        };
    }]);

}(this));
//注册转发器

(function (global) {
    "use strict";

    angular.module("TestApp", [])
           .config([ '$httpProvider',function ($httpProvider) {
                /* Register the interceptor */
                $httpProvider.interceptors.push('httpInterceptor');

    }]);

}(this));

有关更多信息,请参阅。

您的语法是正确的,但要清楚,它不会捕获getDataUriForUrl中的异常,而是捕获失败。这意味着您必须在getDataUriForUrl中显式调用$q.reject。因此,请将承诺中的成功和失败视为.resolve和.reject的处理程序。服务或api的创建者必须调用其中一个函数。编辑:一个简单的修复方法是捕获服务内部的错误并简单地调用$q。非常感谢,这帮助我解决了问题。您使用的语法是正确的,但要清楚,它不会捕获getDataUriForUrl中的异常,而是捕获失败。这意味着您必须显式地调用$q.reject在getDataUriForUrl中。因此,请将承诺中的成功和失败视为.resolve和.reject的处理程序。服务或api的创建者必须调用其中一个函数。编辑:一个简单的修复方法是捕获服务中的错误,只需调用$q。RejectOK非常感谢,这帮助我解决了问题。语法y您拥有的是正确的,但要清楚,它不会捕获getDataUriForUrl中的异常,而是捕获失败。这意味着您必须在getDataUriForUrl中显式调用$q.reject。因此,请将承诺中的成功和失败视为.resolve和.reject的处理程序。服务或api的创建者必须调用其中一个函数。Edit:Simple修复方法是捕获服务中的错误,然后简单地调用$q.reject。非常感谢,这帮助我解决了问题。您的语法是正确的,但要清楚,它不会捕获getDataUriForUrl中的异常,而是捕获失败。这意味着您必须在getDataUriForUrl中显式调用$q.reject。因此,请考虑成功服务或api的创建者必须调用其中一个函数。编辑:一个简单的修复方法是捕获服务内部的错误并简单地调用$q.RejectOK非常感谢,这帮助我解决了这个问题。