When the element is cleared from the page by ng-switch
, two things should happen:
The scope created for
ng-switch-when
, the element with your directive on, is destroyed. This kills your$watch
and generates a$destroy
event across the scope that you can watch withscope.$on('$destroy', ...)
.The element is removed from the DOM. This generates a separate destroy event that you can watch with
iElement.on('$destroy', ...)
.
They should happen in this order, looking at the latest stable release (1.0.8 - https://github.com/angular/angular.js/blob/v1.0.8/src/ng/directive/ngSwitch.js), so your scope and thus your watch should always be dead when the element is removed from the DOM.
You could avoid this problem by watching from the outer scope, where ng-switch
is defined. Or you could watch dataStatus
, the same condition as in your ng-switch
, rather than looking for the results of the ng-switch
seeing your condition change.
Both of these would probably work, but actually all you need to do, and in fact the normal pattern for this, is to just watch for one of the $destroy
events and clean everything up there. As the interval feels more relevant to the view than the model, I would use the DOM event and replace your $watch
with
iElement.on('$destroy', function(){
clearInterval(spinner);
});