// 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/