Angularjs资源构建错误的资源URL

Upv*_*ote 1 angularjs angularjs-resource

资源:

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;
}]); 
Run Code Online (Sandbox Code Playgroud)

查询:

$scope.userTickets = Ticket.listByOwner({
    id :  $rootScope.user.id
}, function(){
    //success
}, function(response){});
Run Code Online (Sandbox Code Playgroud)

结果:

在此输入图像描述

Angularjs构建了一个错误的URL,/api/tickets但它应该是/api/tickets/2/owner.有什么想法吗?

joa*_*mbl 5

@指示角应该寻找数据对象,这是在票务服务方法的第二个参数(可选)上的属性.在第一个参数中,指定请求参数.有两种方法可以解决这个问题:

  • 添加一个空对象作为第一个参数
$scope.userTickets = Ticket.listByOwner({},{
  id :  $rootScope.user.id
}, function(){
  //success
}, function(response){});
Run Code Online (Sandbox Code Playgroud)
  • 或重命名请求参数对象键(从idid1):
$scope.userTickets = Ticket.listByOwner({
  id1 :  $rootScope.user.id
}, function(){
  //success
}, function(response){});
Run Code Online (Sandbox Code Playgroud)