1

最初の AngularJS コントローラーをテスト アプリケーションに実装していますが、いくつか問題があります。

RESTful Web サービスから取得するアイテムのリストがあり、このアイテムに基本的な CRUD ロジックを実装しようとしています。

リストはitemsList.htmページに表示され、ルーティング構成は次のとおりです。

app.config(function ($routeProvider) {
    $routeProvider
        .when('/items',
            {
                controller: 'ItemsController',
                templateUrl: 'app/Partials/itemsList.htm'
            })
        .when('/items/:itemID',
            {
                controller: 'ItemsController',
                templateUrl: 'app/Partials/editItem.htm'
            })
        .otherwise({ redirectTo: '/items' });
});

それから私は私のものを持っていますItemsController:

app.controller('ItemsController', function ($scope, $routeParams, $location, itemsService) {
    $scope.status;
    $scope.items;

    getItems();

    function getItems() {
        itemsService.getItems()
            .success(function (items) {
                $scope.items = items;   
            })
            .error(function (error) {
                $scope.status = 'Unable to load items: ' + error.message;
            });
    }

    function getItem(ID) {
        // ...  
    }
}

同じコントローラーに、ID を渡して特定のアイテムを返し、editItem.htmページに入力する関数を追加したいのですが、この関数にアクセスする方法がわかりません...

/items/:itemIDつまり、この関数へのパスをどのようにマップできますか? 別のコントローラーに実装して、ルート構成を変更する必要がありますか?

: 私は通常、Java と Spring MVC を使用して Web アプリを実装し、通常、アプリケーションのエンティティごとに 1 つのコントローラーを実装します (たとえば、ItemsController、CustomersController など)。AngularJs でもこのルールに従うのは正しいですか、それとも他のベスト プラクティスがありますか?

4

1 に答える 1