Angularjs Resource Generates Invalid Resource URL

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 is building an invalid url, /api/tickets , but it should be /api/tickets/2/owner . Any ideas why?

+1
source share
1 answer

@ indicates that angular should look up the attribute of the data object, which is the second parameter (optional) in Ticket service methods. The first parameter sets the query parameters. There are two ways to fix this:

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

Source: https://habr.com/ru/post/1202633/


All Articles