0

ページでアクティブなコントローラーが 2 つあります。

// For handling any changes made to the Recipe Window
      ctrl.controller('recipeCtrl', ['$scope', 'view_service', 'recipe_service', function($scope, view_service, recipe_service) {
          $scope.title = recipe_service.get_title();
      }]);

      ctrl.controller('setNameCtrl', ['$scope', 'view_service', 'recipe_service', function($scope, view_service, recipe_service) {
          $scope.titleSet = recipe_service.get_title();
          $scope.setName = function(){
              recipe_service.set_title($scope.titleSet);
              //view_service.set_view_url({url:"partials/typeWindow.tpl.html"});
          };
      }]);

両方のコントローラーがこのサービスからプルしています。

serv.service('recipe_service', function(){
    var recipe = {
                        title:"ace",
                        type:"",
                        market:[],
                        attribute:[]
                        };

    return {
        get_title: function() {
            return recipe.title;
        },
        set_title: function(newTitle){
            recipe.title = newTitle;
        }      
    };
});

2 番目のコントローラーは、最初のコントローラーが参照している「タイトル」を更新します。私の問題は、2 番目のコントローラーがサービスで「タイトル」を変更すると、最初のコントローラーが更新されず、変更が反映されないことです。私が考える必要があるのは、最初のコントローラーを更新して新しい変更を取り込む方法です。その方法について何か提案はありますか?

4

2 に答える 2

0

以下のプランカーを見つけてください

http://plnkr.co/edit/UQgAC8bzcJkmsfCMyP84?p=preview

于 2014-02-18T20:42:06.137 に答える
0
// For handling any changes made to the Recipe Window
  ctrl.controller('recipeCtrl', ['$scope', 'view_service', 'recipe_service', function($scope, view_service, recipe_service) {
      $scope.curRecipe = recipe_service.recipe;
  }]);

  ctrl.controller('setNameCtrl', ['$scope', 'view_service', 'recipe_service', function($scope, view_service, recipe_service) {
      $scope.curRecipe = recipe_service.recipe;
      $scope.setName = function(){
          //view_service.set_view_url({url:"partials/typeWindow.tpl.html"});
      };
  }]);

serv.service('recipe_service', function(){

    return {
        recipe : {
                   title:"ace",
                   type:"",
                   market:[],
                   attribute:[]
                 },
        set_title: function(newTitle){
            recipe.title = newTitle;
        }      
    };
});

全体で同じオブジェクトを使用するとうまくいきます...このオブジェクトはサービスで定義するか、使用できます

 angular.module("myApp",[]).value("myObject","Shared Value");

次に、これを次のようにコントローラーに挿入します

angular.module("myApp").controller("MyCtrl",function($scope,myObject){console.log(myObject)})

また、いくつかのオプションを示す plnkr をしばらく前に投稿しました。

http://plnkr.co/edit/ABQsAxz1bNi34ehmPRsF?p=preview

于 2014-02-18T19:43:37.190 に答える