$リソースの$ SAVEに変更されたフィールドのみを投稿する
-
12-12-2019 - |
質問
NGResourceに$ SAVEを呼び出すときは、 onl のみを毎回投稿するのではなくのみを投稿することができますか?
var User = $resource('http://example.com/user/123/');
User.get(function(user) {
user.name="John Smith";
user.$save();
// What I *want* -> POST: /user/123/ {name:'John Smith'}
// What currently happens -> POST: /user/123/ {name:'John Smith', age: 72, location: 'New York', noOfChildren: 5}
});
. 解決
いいえ、少なくともインスタンスではない、 http:// docsを参照してください。angularjs.org/api/ngresource.reesource
[...]クラスオブジェクトまたはインスタンスオブジェクトのアクションメソッドは、次のように呼び出すことができます。 パラメータ:
- http "クラス"アクション:
Resource.action([parameters], [success], [error])
- get "クラス"アクション:
Resource.action([parameters], postData, [success], [error])
- GETインスタンスアクション:
instance.$action([parameters], [success], [error])
それで、データを「静的」の保存方法、つまりUser.save
に保存するようにデータを渡すことによってのみ可能です。このようなもの:
User.get(function(user)
{
user.name = 'John Smith';
User.save({name: user.name});
});
.
これがうまくいっているかどうかはおそらくuser
インスタンスを使用して何をするのかに依存します。
他のヒント
1つのフィールドのみを保存したい場合は、静的.save()
メソッドを使用して、それからの応答をとるコールバックを使用して、Successでローカルオブジェクトを更新します。
$scope.saveOneField = function(modelInstance) {
ModelName.save({
id: modelInstance.id,
theField: <some value>
}, function(response) {
// If you want to update *all* the latest fields:
angular.copy(response, modelInstance.data);
// If you want to update just the one:
modelInstance.theField = response.data.theField;
});
};
.
POSTリクエストがリソース(/modelnames/:id
)に送信されると、サーバーはModelInstaceの最新の更新版で応答します。
所属していません StackOverflow