用Angular观看工厂变量 [英] watch factory variable with Angular
问题描述
我的单页应用程序有2个控制器:第一个用于我的主菜单,第二个用于视图.他们与此工厂共享数据
My single page application has 2 controllers : the first is for my main menu and the second is for the view. They share data with this factory
myApp.factory('MenuFactory', function(){
var factory = {
Monitor: "doneJob",
Control: "",
Report: "",
Display: "",
setMonitor: function(value){
factory.Monitor = value;
},
setControl: function(value){
factory.Control = value;
},
setReport: function(value){
factory.Report = value;
},
setDisplay: function(value){
factory.Display = value;
}
};
return factory;
});
我想观看factory.Control
上的更改,但是我无法使其生效.
I would like to watch change on factory.Control
, but i can't make it works.
当我尝试这样做时:
$scope.$watch(function(){MenuFactory.Control}, function(NewValue, OldValue){
console.log(NewValue + ' ' + OldValue);
console.log(MenuFactory.Control);
}, true);
我在控制台中得到"undefined undefined"和"Process".我在该工厂的$ watch实施有什么问题吗?
I get "undefined undefined" and "Process" in console. Is there any problem with my $watch implementation for this factory ?
推荐答案
语法错误.它应该return
服务变量MenuFactory.Control
&最后,也不需要true
,因为您没有对象来检查对象的相等性.
You had a wrong syntax. It should return
the service variable MenuFactory.Control
& also there is no need of true
in the end as you don't have object to check object equality.
代码
$scope.$watch(function(){
return MenuFactory.Control;
}, function(newValue, oldValue){
console.log(newValue + ' ' + oldValue);
console.log(MenuFactory.Control);
});
这篇关于用Angular观看工厂变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!