一个服务里面看$?

一个服务里面看$?

问题描述:

是否有可能建立一个 $观看里面有服务对象的数组(我想在 $观看声明本身是服务中)?

Is it possible to set up a $watch on an array of objects inside a service (I'd like the $watch declaration itself to be inside the service)?

您可以通过注入添加任何前pression设定的手表 $ rootScope 和使用函数的第一个参数 $观看方法。伪code:

You can add any expression to the set of watches by injecting the $rootScope and using a function a first argument to the $watch method. Pseudo-code:

$rootScope.$watch(function() {
  return //value to be watched;
}, function watchCallback(newValue, oldValue) {
  //react on value change here
});

不要忘了第三个,布尔参数传递给 $观看方法。您需要指定真正如果你想深监视更改整个对象。

Don't forget about the third, Boolean argument to the $watch method. You need to specify true if you want to deep-watch an entire object for changes.