如何在 AngularJS 中停止 $watching? [英] How do I stop $watching in AngularJS?

查看:19
本文介绍了如何在 AngularJS 中停止 $watching?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在 AngularJS 作用域上设置一个 $watch,以便在我感兴趣的表达式发生变化时收到通知.但是,一旦失去兴趣,我该如何停止观看?

I can set up a $watch on an AngularJS scope to be notified when the expression I am interested in has changed. But how do I stop watching once I lose interest?

推荐答案

当调用 $watch 时,会返回一个取消注册绑定表达式的函数.

When calling $watch a function is returned that unregisters the bound expression.

例如,观察一个变量 foo 只改变一次:

E.g., to watch a variable foo only change once:

var unregister = $scope.$watch('foo', function () {
  // Do something interesting here ...
  unregister();
});

希望有帮助:-)

这篇关于如何在 AngularJS 中停止 $watching?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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