我的页面上有两个活动的控制器:
// 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;
}
};
});
第二个控制器更新第一个控制器引用的“标题”。我的问题是,一旦第二个控制器更改了服务中的“标题”,第一个控制器就不会更新以反映更改。我认为需要发生的是如何刷新第一个控制器来引入这些新的变化。有关如何这样做的任何建议吗?
答案 0 :(得分: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,显示了一些选项:
答案 1 :(得分:0)
请找到下面的plunker