资源:
angular.module('TicketService', ['ngResource'])
.factory('Ticket', ['$resource', function($resource){
var Ticket = $resource('/api/tickets/:id1/:action/:id2',
{
id1:'@id'
},
{
list: {
method: 'GET'
},
listByOwner: {
method: 'GET',
params: {
action:'owner',
id1:"@id"
}
}
update: {
method: 'PUT',
params:{}
}
});
return ticket;
}]);
查询:
$scope.userTickets = Ticket.listByOwner({
id : $rootScope.user.id
}, function(){
//success
}, function(response){});
结果:
Angularjs构建了错误的网址/api/tickets
,但它应该是/api/tickets/2/owner
。有什么想法吗?
答案 0 :(得分:5)
@
表示angular应该在数据对象上查找属性,这是Ticket服务方法中的第二个参数(可选)。在第一个参数中,指定请求参数。有两种方法可以解决这个问题:
$scope.userTickets = Ticket.listByOwner({},{
id : $rootScope.user.id
}, function(){
//success
}, function(response){});
id
到id1
):$scope.userTickets = Ticket.listByOwner({
id1 : $rootScope.user.id
}, function(){
//success
}, function(response){});