Rothschild Rothschild - 27 days ago 7
AngularJS Question

Binding scroll event - how to unbind?

I am using the following code to bind scroll on a page;

angular.element($window).bind("scroll", function () {});


The issue I am having is that the binding is persisting when I navigate away from the page. How do I release a binding? I tried assigning it to a variable and calling said variable in the binding, like with
$on()
but that didn't work.

Answer

First namespace your scroll event, so you can remove it specifically when the need arises: angular.element($window).bind("scroll.myScroll", function() {});

Then unbind it:
angular.element($window).unbind("scroll.myScroll");

"myScroll" is a namespace you chose.