Michael Michael - 5 months ago 23
AngularJS Question

Unbinding $watch in angularjs after called

I know you can unbind a $watch like this:

var listener = $scope.$watch("tag", function () {});
// ...
listener(); // would clear the 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()
});

Answer

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

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