2013-05-25 36 views
1

AngularJS documentation中,似乎只能保存现有的对象,而不是新的对象。也就是说,在$resource上有一个采用零参数的$save方法。

如何对我POST a 对象使用$resource对服务器?

该文件严重缺乏。

回答

3

Resource.save将该对象作为参数。

http://docs.angularjs.org/api/ngResource.$resource

var Item = $resource("/api/items/:id", {id: "@id"}); 
//Item is the "model", you have the method unprefixed 
$scope.items = []; 
$scope.addItem = function() { 
    var item = Item.save({name: $scope.newItemName}); //unprefix call 
    $scope.items.push(item); 
    $scope.newItemName = ''; 
}; 
$scope.saveItem = function (item) { 
    // item is, this time, a "Item instance" (returned by Item.save) 
    // and has the functions prefixed 
    item.$save(); 
} 

从我们的聊天,似乎这已经足够了(最低限度)以POST对象:

$resource('/some/path').save({name: 'Fred'}); 
+0

什么是'Item'? –

+1

这是资源 – Ven

+1

这是一个'$资源'?你的意思是'$ save'而不是'save'吗?你是如何获得资源的?什么是“查询”?我很困惑。这里没有任何背景。 –