admin管理员组文章数量:1356218
How do I use an interceptor
within an Angular $resource
?
My JSON structure:
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]);
}
}
});
My services, and what I tried using the interceptor
object:
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?
How do I use an interceptor
within an Angular $resource
?
My JSON structure:
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]);
}
}
});
My services, and what I tried using the interceptor
object:
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?
Share Improve this question edited Oct 9, 2013 at 12:57 Shikiryu 10.2k9 gold badges52 silver badges76 bronze badges asked Sep 3, 2013 at 1:47 AlfredAlfred 1111 silver badge6 bronze badges1 Answer
Reset to default 6Your service is improperly formed. There are curly braces and parentheses int the wrong places.
Here is the correct version (modified slightly so that I could get it to run: http://jsfiddle/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
}
}
);
});
本文标签: javascriptUsage of interceptor within resourceStack Overflow
版权声明:本文标题:javascript - Usage of interceptor within $resource - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744046373a2581600.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论