2013-03-03 11 views
7
// 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"} 

変更された属性で/ users/1へのPUTが発生することが予想されます。しかし、代わりに/ usersへのPOSTであり、新しいユーザー(新しいIDと新しい名前)が作成されます。

私が間違っていることはありますか?

あなただけの彼は、ルート・パラメータをバインドするために持っているか、$リソースを指示する必要があり
AngularJS v1.0.5 

答えて

8

":ID" JSONオブジェクトを持つ:

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

これは動作するはずです、 よろしく

+1

ありがとうございます.90%ありましたら、更新されたユーザーで/ users/1にPOSTします。しかし、今はPOSTの代わりにPUTを作成する方法は?とても近い。 –

+0

私はPOSTを設計していると思います。新しいリソースの場合はPOSTに、既存のリソースへの更新の場合はPUTにオーバーライドする必要があります。 http://docs.angularjs.org/api/ngResource.$resource –

+15

POST、$ resource( "/ users /:id"、{'id': '@id'}の代わりにPUTに関する質問に答えるには、 {更新:{メソッド: 'PUT'}}); PUTのトリックを行います。 User.sup(user)とuser。$ saveのやり方で、User.update(user)またはuser。$ updateを呼び出すだけです。 –

0
答えのために

ありがとう。それは私のためにも最後に働いた。 補足として、.NET Web APIを使用していて、エンティティにIDプロパティ(大文字の「I」)があります。私は次のように使用した後 PUTとDELETEのみが働いた:

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

はそれがお役に立てば幸いです。

+1

.NETを使用しても、{{Id: '@ Id'} ' – Arion

関連する問題