質問

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の最新の更新版で応答します。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top