6

AngularJS は初めてで、フレームワークを理解しようとしており、基本的な CRUD アプリを構築しようとしています。既存のレコードを更新するために何が必要なのかわかりません。これが私のサービスです:

 angular.module('appServices', ['ngResource']).
factory('App', function ($resource) {
    var Item = $resource('App/:AppId', {
        //Default parameters
        AppId: '@id'
    }, {
        //Actions
        query: {
            method: 'GET',
            isArray: true
        },
        getById: {
            method: 'PUT'
        },
        update: {
            method: 'POST'
        }
    });
    return Item;
});

基本的な Get all クエリと getById を実行して編集フォームにデータを入力することはできますが、ここで行き詰まっています。getById のコード例を次に示します。

 $scope.apps = App.query();

$scope.getEdit = function(AppId) {
    App.getById({id:AppId}, function(app) {
        $scope.original = app;
        $scope.app = new App(app);
    });
};

$scope.save = function() {
    //What type of information should go here?
    //Do I need to make changes to the appServices?
};

私は、既存の情報の更新に関する次のこと、または「アプリ」オブジェクトがAPIにどのように渡されるかがわからないだけだと思います.誰かが私を正しい方向に向けるか、簡単な更新方法を教えてもらえますか?

4

2 に答える 2

4

もう少し調査を行い、ダニエルのリンクを確認した後 (ありがとう)。私はそれを働かせました。

コントローラーの方法:

 $scope.save = function() {
    $scope.app.update();
};

サービス工場:

 var Item = $resource('App/Details/:AppId', {
        //Default parameters
        AppId: '@id'
    }, {
        //Actions
        query: {
            method: 'GET',
            isArray: true
        },
        getById: {
            method: 'PUT'
        },
        update: {
            method: 'POST'
        }
    });

    Item.prototype.update = function (cb) {
        console.log(this.AppId);
        return Item.update({ AppId: this.AppId },
        angular.extend({}, this, { AppId: undefined }), cb);
    };

    return Item;
于 2012-08-21T12:42:17.857 に答える