How to prevent / untie previous $ watch in angularjs

I use it $watchdynamically, so every call invokes its creation yet $watch, while I want to untie the previous one $watch.

$scope.pagination =function(){
  $scope.$watch('currentPage + numPerPage', function (newValue,oldValue) {      
    $scope.contestList = response; //getting response form $http
  }
}

I have several tabs. When you click on the tab, the pagination function is called:

$scope.ToggleTab = function(){
    $scope.pagination();
}

so it creates several $watch, and every time I click on the tab, it creates another one.

+4
source share
1 answer

, . , watcher(), $scope.$$watchers. . , .

var paginationWatch;
$scope.pagination = function() {
    if (paginationWatch) //check for watch exists
        paginationWatch(); //this line will destruct watch if its already there
    paginationWatch = $scope.$watch('currentPage + numPerPage', function(newValue, oldValue) {
            //getting response form $http`
            $scope.contestList = response;
        }
    });
}
+6

All Articles