1

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:

enter image description here

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

2
  • Are you sure $rootScope.user.id isn't undefined? Commented May 21, 2013 at 22:30
  • @MarekTakac nope it was set Commented May 22, 2013 at 4:02

1 Answer 1

5

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){});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.