2013-05-25 39 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'嗎?你是如何獲得資源的?什麼是「查詢」?我很困惑。這裏沒有任何背景。 –