加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何处理AngularJS中的$resource service错误

发布时间:2020-12-17 09:11:36 所属栏目:安全 来源:网络整理
导读:我向我的API请求,我使用AngularJS $资源模块。它不同于$ http,所以我不知道如何处理我的错误。 我的服务: var appServices = angular.module('app.services',['ngResource']);appServices.factory('Category',['$resource',function($resource){ return $
我向我的API请求,我使用AngularJS $资源模块。它不同于$ http,所以我不知道如何处理我的错误。

我的服务:

var appServices = angular.module('app.services',['ngResource']);
appServices.factory('Category',['$resource',function($resource){
        return $resource('/apicategoryerr/?format=:format',{},{
            query: {
                method: 'GET',params: { format: 'json'},isArray: true,}
        });
    }]);

我的控制器:

...
Category.query(function(data) {
                console.log(data);
            });
...

我想要的东西像这样或..我不知道一种方法来处理错误,如果我的API不工作..

Category.query().success(function() {
                console.log('success');
            }).error(function() {
                console.log('error');
            });
您可以将错误处理程序作为第二个参数toquery。
Category.query(function(data) {},function() {});

编辑:

使事情更清楚,一些例子:

var Resource = $resource('/restapi/resource');

Resource.query(function(data) {
    // success handler
},function(error) {
    // error handler
});

Resource.query({
    'query': 'thequery'
},function(data) {
    // success handler
},function(error) {
    // error handler
});

Resource.query().$promise.then(function(data) {
    // success handler
},function(error) {
    // error handler
});

Resource.query({
    'query': 'thequery'
}).$promise.then(function(data) {
    // success handler
},function(error) {
    // error handler
});

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读