Angularjs 关于对象……不考虑添加属性更改
在Angular.js中,我的指令中有以下内容:Angularjs 关于对象……不考虑添加属性更改,angularjs,angularjs-directive,Angularjs,Angularjs Directive,在Angular.js中,我的指令中有以下内容: scope.$watchCollection(function(){ return StatusTrackerService.eventCollection }, function(){ console.log("ssssssssssss"); console.log(StatusTrackerService); console.log("ssss
scope.$watchCollection(function(){
return StatusTrackerService.eventCollection
}, function(){
console.log("ssssssssssss");
console.log(StatusTrackerService);
console.log("ssssssssssss");
});
在我的服务中,我向它提供
StatusTrackerService.eventCollection
,并向eventCollection
添加一个属性:
function runEventCheck(account, eventId) {
url = urlBuilder(account, eventId);
eventCollection[referenceVipLabel.vipLabel] = new NewStatusEvent(account, eventId, url);
}
return { runEventCheck: runEventCheck,
eventCollection: eventCollection,
referenceVipLabel: referenceVipLabel
};
});
将属性添加到
eventCollection
时,$watch
不会检测到更改。在我看来,将属性添加到对象eventCollection
会改变对象。为什么$watch
没有检测到此更改?我真的不想使用$watchCollection
,因为这样它会检测到对StatusTrackerService
的任何更改,您需要对该属性执行深度监视。您可以通过设置手表上的第三个参数来实现
scope.$watch(function(){
return StatusTrackerService.eventCollection;
}, function(v){
console.log(v);
}, true); //<-- Here
scope.$watch(函数(){
返回StatusTrackerService.eventCollection;
},功能(五){
控制台日志(v);
},对)//eventCollection
是StatusTrackerService.eventCollection
对吗?是,eventCollection是StatusTrackerService.eventCollection