Angularjs resource builds wrong resource url

家住魔仙堡 提交于 2019-12-24 18:55:23

问题


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;
}]); 

Query:

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

Result:

Angularjs builds a wrong url, /api/tickets but it should be /api/tickets/2/owner. Any ideas why?


回答1:


The @ indicates that angular should look for the attribute on the data object, which is the second parameter (optional) in the Ticket service methods. In the first parameter you specify the request parameters. There are two ways you can fix this:

  • Add an empty object as the first parameter
$scope.userTickets = Ticket.listByOwner({},{
  id :  $rootScope.user.id
}, function(){
  //success
}, function(response){});
  • Or rename the request parameter object key (from id to id1):
$scope.userTickets = Ticket.listByOwner({
  id1 :  $rootScope.user.id
}, function(){
  //success
}, function(response){});


来源:https://stackoverflow.com/questions/16677487/angularjs-resource-builds-wrong-resource-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!