2

Is there any risk of doing that? Example:

if (someCondition) {
   angular.element($window).bind('scroll', myHandler);
}

$scope.$on('$destroy', function() {
    angular.element($window).unbind('scroll', myHandler);
});

Of course I could do

$scope.$on('$destroy', function() {
    if (someCondition) {
        angular.element($window).unbind('scroll', myHandler);
    }
});

But I don't know if it's necessary. I've tested without the someCondition condition and "it works", but I wanted to be sure.

Marcos
  • 4,643
  • 7
  • 33
  • 60
  • I think angular add DOM property ng339, so you may check that as a workaround – YOU Sep 16 '15 at 10:19
  • Well, not sure I get it. Workaround? What I've posted actually works, just wanted to know if it's ok to do it in that way. Thanks. – Marcos Sep 16 '15 at 10:28
  • 1
    There is no error from unbinding a handler that is not bond, not 100% kosher, but it won't cause any errors. See http://stackoverflow.com/a/2518441/227299 – Ruan Mendes Sep 16 '15 at 11:15

1 Answers1

1

Its absolutely fine, there is no need to check the condition while destruction of scope.

if (someCondition) {   angular.element($window).bind('scroll', myHandler);}

$scope.$on('$destroy', function() { angular.element($window).unbind('scroll', myHandler);});
Kashif Mustafa
  • 1,152
  • 6
  • 20