admin管理员组文章数量:1434924
I am using $watch
in dynamic way so on every call its creating another $watch
while I want to 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 a tab is clicked, the pagination function gets called:
$scope.ToggleTab = function(){
$scope.pagination();
}
so its creating multiple $watch
and every time I click on tab it creates another one.
I am using $watch
in dynamic way so on every call its creating another $watch
while I want to 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 a tab is clicked, the pagination function gets called:
$scope.ToggleTab = function(){
$scope.pagination();
}
so its creating multiple $watch
and every time I click on tab it creates another one.
1 Answer
Reset to default 8Before adding a new watch you need to check if the watcher exists already or not. If it's already there you could just call watcher()
this will remove the watcher from $scope.$$watchers
array. And then register your watch. This will ensure your watch has been created only once.
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;
}
});
}
本文标签: javascriptHow to preventunbind previous watch in angularjsStack Overflow
版权声明:本文标题:javascript - How to preventunbind previous $watch in angularjs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745627486a2667062.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论