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

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

问题描述

我的页面上有两个处于活动状态的控制器:

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=preview

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

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