2016-05-16 126 views
0

我有一个工厂用于更新我的休息服务中的数据。如果我硬编码id在它的更新就好了,如果我试图通过我的json文件paremeter,json传递给其余服务,但paremeter没有被传递到工厂,所以我的restUrl不正确地生成为缺少itemId。从Angularjs中的控制器传递JSON和参数到工厂

厂:

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和参数的组合时,它不会显示如何执行它。

回答

1

Angular docs

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

所以看起来你应该通过ID作为对象,并交换参数

+0

是啊,我的东西转身。感谢您指出了这一点! :)所以,我的更新现在看起来像这样 - updateRfp.update({itemId:$ scope.itemId},$ scope.itemData) - 为了任何其他人可能正在努力的好处。 –

相关问题