I have a directive that can trigger a bootstrap popover when I hover on it, I've noticed the $$watchers number increases every time after I hover on it, and it won't decrease even after the popover is removed, this is causing a memory leak in my application as this directive is in a list and doesn't get destroyed until user logs out. My question is how can I remove these watchers to prevent the memory leak?
This is the directive that can trigger the popover
return module.directive('listItem', function () {
return {
restrict: 'E',
replace: true,
templateUrl: '/list-item.html',
scope: {
listItem: '='
}
};
});
This is the list-item.html, my-popover is nothing but a ng bootstrap popover decorator.
<div class="list-item"
my-popover
trigger="hover"
placement="bottom"
content-template="/list-item-popover.html">
This is list-item-popover.html
<div id="list-item-popover">
<div id="saved-item-row">
<i ng-class="listItem.examSaved ? 'icon-ico_star_sm' : 'icon-ico_starempty_sm' "></i>
</span>
</div>
</div>
As you can see above template has a ng-class, I saw a watcher is added to the $$watchers array under scope every time popover is displayed. After hovering over on it for 16 times, I saw 16 duplicate objects in the array. I am wondering if there is a way to prevent duplicate objects being added?