本文介绍了$watch 在服务中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在服务内的对象数组上设置 $watch(我希望 $watch 声明本身在服务内)?

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)?

推荐答案

您可以通过注入 $rootScope 并使用函数作为 的第一个参数,向监视集添加任何表达式>$watch 方法.伪代码:

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
});

不要忘记 $watch 方法的第三个布尔参数.如果要深入观察整个对象的更改,则需要指定 true.

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.

这篇关于$watch 在服务中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 18:27