我正在向我的 API 发出请求,并且正在使用 AngularJS $resource 模块。它与 $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,
}
});
}]);
我的 Controller :
...
Category.query(function(data) {
console.log(data);
});
...
我想要这样的东西或者..如果我的API不工作,我不知道如何处理错误..
Category.query().success(function() {
console.log('success');
}).error(function() {
console.log('error');
});
最佳答案
您可以将错误处理程序作为第二个参数传递给查询
。
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
});
关于angularjs - 如何处理 AngularJS 中的 $resource 服务错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20584367/