Angularjs资源建立错误的资源链接 [英] Angularjs resource builds wrong resource url

查看:219
本文介绍了Angularjs资源建立错误的资源链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

资源:

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建立了一个错误的URL, / API /票,但它应该是 / API /票/ 2 /所有者。任何想法,为什么?

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

推荐答案

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

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:


  • 添加一个空对象作为第一个参数

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


  • 或重命名请求参数对象键( ID ID1

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

      这篇关于Angularjs资源建立错误的资源链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆