Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 关于对象……不考虑添加属性更改_Angularjs_Angularjs Directive - Fatal编程技术网

Angularjs 关于对象……不考虑添加属性更改

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

在Angular.js中,我的指令中有以下内容:

    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