4

$resource サービスを使用して query() メソッドを使用してデータを取得し、model.$save() メソッドを使用して新しいデータを作成する AngularJS アプリケーションがあります。これは、ドキュメントが言うようにうまく機能します。

私の質問は、MyService.query() を使用して取得したローカル データを変更した後、最初に更新する方法です。

今回は最も単純な方法を採用し、query() メソッドをもう一度呼び出すだけです。これが効率的には最悪の方法であることは知っていますが、最も単純な方法です。

私のサーバー側では、新しいモデルの状態表現全体を返します。新しく作成したモデルをローカル データの配列に追加するにはどうすればよいですか?

更新
私は単純にモデルのリターンをサーバーからプッシュすることになりましたが、それでもそれが正しい方法であるかどうかを知りたいです. ソースコードから理解できることから、返される配列は、自分で操作できる plan-old-javascript-array です。

これは私が使用したコードです

$scope.save = function () {
    var newComment = new CommentsDataSource();
    newComment.Content = $scope.todoText;
    newComment.$save({ id: "1" }, function (savedComment) {
        $scope.comments.push(savedComment);
    });
}
4

1 に答える 1

3

I would simply get the whole list again, to be able to see the modifications brought to the list by other users.

But if the solution you're using suits you, then use it. It's corrrect. Angular uses bare-bones JavaScript objects. Adding a new instance to a list in the scope will refresh the list displayed on the page.

于 2013-07-20T14:09:44.120 に答える