Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
196
rated 0 times [  198] [ 2]  / answers: 1 / hits: 17668  / 11 Years ago, wed, october 16, 2013, 12:00:00

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

More From » angularjs

 Answers
19

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



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

[#74963] Tuesday, October 15, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
brookelynshaem

Total Points: 468
Total Questions: 98
Total Answers: 101

Location: China
Member since Mon, Aug 22, 2022
2 Years ago
;