如何interceptor
在 Angular 中使用 an $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'}]
},
{ ... },
{ ... }
];
我的控制器如下:
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]);
}
}
});
我的服务,以及我尝试使用该interceptor
对象的方法:
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
}
);
});
我读了 $resource,似乎这应该有效,但它没有,所以我误解了。有什么建议么?