alalonde / angular-scrollable-table

A fixed header table directive for AngularJS
MIT License
68 stars 48 forks source link

Event listeners never removed from window object #51

Closed jdmarsh closed 7 years ago

jdmarsh commented 7 years ago

Inside the directive there is this section of code:

angular.element(window).on('resize', function(){
    $timeout(function(){
        $scope.$apply();
    });
});

This adds an event listener onto the window object every time the directive scope is created. If you have an application with multiple tables over multiple pages then the event listeners soon start adding up.

Suggest doing the following or similar:

var onResizeCallback = function(){
    $timeout(function(){
        $scope.$apply();
    });
};
angular.element(window).on('resize', onResizeCallback);
$scope.$on('$destroy', function() {
    angular.element(window).off('resize', onResizeCallback);
});