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.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;
        }      
    };
});

第二个控制器更新第一个控制器引用的“标题”。我的问题是,一旦第二个控制器更改了服务中的“标题”,第一个控制器就不会更新以反映更改。我认为需要发生的是如何刷新第一个控制器以引入这些新更改。关于如何做到这一点的任何建议?

4

2 回答 2

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 回答
0

请找到下面的plunker

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

于 2014-02-18T20:42:06.137 回答