1

カテゴリリストを取得するために注入されたcategoryData $resourceサービスを持つcategoryListControllerからカテゴリのリストをロードするサイドバー(index.html上)を備えたSPAに角度ルーティングを使用しています。

それから、categoryData $resource サービスも使用する addCategoryController の助けを借りてカテゴリを追加するテンプレート addCategory.html があります。

 $scope.categories = categoryData.query(); //categoryListController (for sidebar)

 categoryData.save(newCategory) // on addCategoryController (for addCategory.html)

問題は、ページ全体を更新しない限りサイドバーが更新されないことです。どうにかして categoryListController を更新するように指示する必要があると考えていますが、その方法がわかりません。$scope.categories.push(newCategory) を categoryData.save(newCategory) の直後に実行して、新しいカテゴリを addCategory.html にすぐに表示することができますが、これが何かでない限り、それが私のサイドバーの答えだとは思いません$rootscope で処理する必要がありますか? わからない。ありがとう

4

2 に答える 2

0

serviceデータ構造を共有し、コンテンツの管理に注意するを作成する必要があります。

このようなもの:

angular.service('categoryService', function() {
  var categories = [], initilized;

  return {
    this.getCategories = function() {
      if (!initialized) {
        // call resource to fulfill the categories array
        initialized = true;
      }

      // you cold return a promise that would be resolved as soon
      // as you get the first response from server
      return categories;
    });

    this.addCategory = function(category) {
      // code to call $resource, add the category and update the
      // categories array, shared between both controllers
      //
      // you could return the promise for adding the content
    });

    this.removeCategory = ...
   };
});

  を呼び出す必要さえありません$resource。このサービスは、永続化の必要性を処理します。もちろん、promise を公開する必要がある場合は、メソッドを変更して追加することもできます。

于 2013-06-08T13:57:35.003 に答える