如何将参数传递给Angular服务?

时间:2013-12-28 09:15:21

标签: javascript angularjs

我有一个Angular服务:

angular.module('transactionService', ['ngResource']).
    factory('Transaction', function($resource){ 
      return $resource('/api/transaction/:transactionId', {}, {
        'query':   {method:'GET',     params:{transactionId : ''},          isArray : true},
        'get':     {method:'GET',     params:{transactionId : '@_id.$oid'}, isArray : false},
        'save':    {method:'POST',    params:{transactionId : ''},          isArray : false},
        'update':  {method:'PUT',     params:{transactionId : '@_id.$oid'}, isArray : false},
      });
});

我称之为:

transaction = {
   _id : { $oid : '20495823405984059' }
}
Transaction.update(transaction, function(d) {});

但是它会获取网址:

/api/transaction/@_id.$oid

而不是:

/api/transaction/20495823405984059

文档显示这是传递参数的正确方法,但它们没有得到插值。我做错了什么?

1 个答案:

答案 0 :(得分:1)

尝试:

angular.module('transactionService', ['ngResource']).
    factory('Transaction', function($resource){ 
      return $resource('/api/transaction/:transactionId', {}, {
        'query':   {method:'GET',     params:{transactionId : ''},          isArray : true},
        'get':     {method:'GET',     params:{transactionId : '@transactionId'}, isArray : false},
        'save':    {method:'POST',    params:{transactionId : ''},          isArray : false},
        'update':  {method:'PUT',     params:{transactionId : '@transactionId'}, isArray : false},
      });
});

这样称呼:

transaction = {
   transactionId : '20495823405984059' //your problem is the key transactionId is missing
}
Transaction.update(transaction, function(d) {});