rest - AngularJS $resource 创建新对象而不是更新对象

标签 rest angularjs

// Set up the $resource
$scope.Users = $resource("http://localhost/users/:id");

// Retrieve the user who has id=1
$scope.user = $scope.Users.get({ id : 1 }); // returns existing user

// Results
{"created_at":"2013-03-03T06:32:30Z","id":1,"name":"testing","updated_at":"2013-03-03T06:32:30Z"}

// Change this user's name
$scope.user.name = "New name";

// Attempt to save the change 
$scope.user.$save();

// Results calls POST /users and not PUT /users/1
{"created_at":"2013-03-03T23:25:03Z","id":2,"name":"New name","updated_at":"2013-03-03T23:25:03Z"}

我希望这会导致 PUT 到/users/1 并更改属性。但它改为 POST 到/users 并创建一个新用户(使用新 ID 和新名称)。

我是不是做错了什么?

AngularJS v1.0.5

最佳答案

你只需要告诉$resource,他如何将路由参数“:id”与JSON对象绑定(bind):

$scope.Users = $resource("http://localhost/users/:id",{id:'@id'});

这应该有效, 问候

关于rest - AngularJS $resource 创建新对象而不是更新对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15192137/

相关文章:

http - 用户拥有的小部件的 URL 的 RESTful 设计

javascript - 无法在 angularJs 中从单击事件的选择中设置第二个下拉列表的选定值

javascript - 动态地将 Angular 属性添加到旧的 html 表单

javascript - 如何使用 underscore.js 找到 javascript 数组中存在的给定值?

rest - 用于非资源响应的 JSON API

api - Rest API 调用,使用 RestSharp 调用 ASP.NET Web API

javascript - 选择框中的 Angular , boolean 值

javascript - AngularJS 中的字符串到对象

python - CherryPy 和 RESTful 网络 API

rest - ColdFusion 和 REST