一个服务里面看$? [英] $watch inside a service?

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

问题描述

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

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.

这篇关于一个服务里面看$?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆