javascript - How to prevent/unbind previous $watch in angularjs -
i using $watch
in dynamic way on every call creating $watch
while want unbind previous $watch
.
$scope.pagination =function(){ $scope.$watch('currentpage + numperpage', function (newvalue,oldvalue) { $scope.contestlist = response; //getting response form $http } }
i have multiple tabs. when tab clicked, pagination function gets called:
$scope.toggletab = function(){ $scope.pagination(); }
so creating multiple $watch
, every time click on tab creates one.
before adding new watch need check if watcher exists or not. if it's there call watcher()
remove watcher $scope.$$watchers
array. , register watch. ensure watch has been created once.
var paginationwatch; $scope.pagination = function() { if (paginationwatch) //check watch exists paginationwatch(); //this line destruct watch if there paginationwatch = $scope.$watch('currentpage + numperpage', function(newvalue, oldvalue) { //getting response form $http` $scope.contestlist = response; } }); }
Comments
Post a Comment