从另一个角度控制器更新控制器 [英] Updating a controller from another controller Angular

查看:168
本文介绍了从另一个角度控制器更新控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个控制器,是活跃在我的网页上:

I have two controllers that are active on my page:

// 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"});
          };
      }]);

两个控制器从这项服务中拉:

Both controllers are pulling from this service:

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

第二控制器更新标题,所述第一控制器被引用。我的问题是,一旦第二控制器在服务改变称号,第一控制器不更新以反映更改。我在想什么,需要发生的是一些如何刷新第一个控制器在这些新的变化来拉。如何任何建议这样做呢?

The second controller updates the "title" that the first controller is referencing. My problem is that once the second controller changes "title" in the service, the first controller is not updated to reflect the changes. What I am thinking that needs to happen is to some how refresh the first controller to pull in those new changes. Any suggestion on how to do so?

推荐答案

请找到下面plunker

Please find the below plunker

http://plnkr.co/edit/UQgAC8bzcJkmsfCMyP84?p=$p$ PVIEW

这篇关于从另一个角度控制器更新控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆