在 Angularjs 中从控制器向工厂传递 JSON 和参数

Passing JSON and Parameters to Factory from Controller in Angularjs

我有一个工厂,我用它来更新我的休息服务中的数据。如果我对其中的 id 进行硬编码更新就好了,如果我尝试将参数与我的 json 文档一起传递,json 将传递给其余服务,但参数不会传递给工厂所以我的 restUrl 没有正确生成,因为它缺少 itemId。

工厂:

angular.module('myApp')
  .factory('updateItem', function ($resource) {

      return $resource('http://someserviceUrl/items/:itemId', {}, {
        update: {method: 'PUT', params: {itemId: '@itemId'}}
      });


  });

控制器:

    $scope.itemId = 21;
    var itemData = {"orderItem":  {"itemHdr": {
          "itemId": "",
          "itemDescription":"Item Description",
          "typeCd": "someType",
          "salesPgm":"thisProgram",
          "onSale": true
        }
};

  updateRfp.update($scope.itemData,$scope.itemId)
    .$promise.then(function(res) {
    console.log('updateItem.update triggered!');
    $scope.results = 'Item '+ res.code + ' has been saved.';


  });

我没有正确传递 itemId 吗?我读过的文档显示了这种在控制器和工厂之间传递变量的方法,但它没有显示在组合 json 和参数时如何进行。

Angular docs

// Now call update passing in the ID first then the object you are updating
Notes.update({ id:$id }, note);

看来您应该将 Id 作为对象传递并交换参数