$资源中的拦截器的用法 [英] Usage of interceptor within $resource
本文介绍了$资源中的拦截器的用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何使用拦截
的角 $资源中的
?
我的JSON结构:
var dgs = [{id :1,
driver:'Sam',
type: 'bus',
segments:[{id:1,origin:'the bakery',arrival:'the store'},
{id:2,origin:'the store' ,arrival:'somewhere'}]
},
{ ... },
{ ... }
];
我的控制器如下:
My controller is the following:
function dgCtrl($scope,$http,DriveGroup,Segment) {
$scope.dgs = DriveGroup.query(function()
// Code below may belong in a response interceptor?
for (var i=0;i<$scope.dgs.length;i++) {
var segments = $scope.dgs[i].segments;
for (var j=0;j<segments.length;j++) {
segments[j] = new Segment(segments[j]);
}
}
});
我的服务,什么我尝试使用拦截
对象:
angular.module('dgService',['ngResource']).
factory("DriveGroup",function($resource) {
return $resource(
'/path/dgs',
{},
{update:{method:'PUT'})
{fetch :{method:'GET',
// This is what I tried.
interceptor:{
response:function(data) {
console.log('response',data);
},
responseError:function(data) {
console.log('error',data);
}
},
isArray:true
}
);
});
我看了 $资源,而且好像这应该工作,但它没有,所以我是错误的认识。有什么建议?
I read $resource, and it seems like this should work, but it doesn't, so I'm mis-understanding. Any suggestions?
推荐答案
您服务不当而形成的。有大括号和括号诠释错误的地方。
Your service is improperly formed. There are curly braces and parentheses int the wrong places.
下面是正确的版本(略作修改,这样我可以得到它的运行:
http://jsfiddle.net/roadprophet/VwS2t/
Here is the correct version (modified slightly so that I could get it to run: http://jsfiddle.net/roadprophet/VwS2t/
angular.module('dgService', ['ngResource']).
factory("DriveGroup", function ($resource) {
return $resource(
'/', {}, {
update: {
method: 'PUT'
},
fetch: {
method: 'GET',
// This is what I tried.
interceptor: {
response: function (data) {
console.log('response in interceptor', data);
},
responseError: function (data) {
console.log('error in interceptor', data);
}
},
isArray: false
}
}
);
});
这篇关于$资源中的拦截器的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文