调用后在angularjs中解除绑定$ watch [英] Unbinding $watch in angularjs after called

查看:265
本文介绍了调用后在angularjs中解除绑定$ watch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道您可以这样取消$ watch的绑定:

I know you can unbind a $watch like this:

var listener = $scope.$watch("tag", function () {});
// ...
listener(); // would clear the watch

但是您可以在watch函数声明中取消绑定手表吗?因此,在手表执行一次后,它会自行解除捆绑吗?像这样:

but can you unbind the watch within the watch function declaration. So after the watch gets executed once, it unbinds itself? Something like:

$scope.$watch("tag", function () {
    unbindme()
});

推荐答案

您可以按照已经执行的方法进行操作,在函数内部调用"deregistration":

you can just do it the same way you already do, call the "deregistration" inside your function:

var unbind = $scope.$watch("tag", function () {
    // ...
    unbind();
});

这篇关于调用后在angularjs中解除绑定$ watch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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