Thanks Sander for your prompt reply.
I don't think I made myself clear. In ngIf, there is a watcher defined:-
$scope.$watch($attr.ngIf, function ngIfWatchAction(value) {
if (value) {
if (!childScope) {
$transclude(function(clone, newScope) {
childScope = newScope;
clone[clone.length++] = document.createComment(' end ngIf: ' + $attr.ngIf + ' ');
// Note: We only need the first/last node of the cloned nodes.
// However, we need to keep the reference to the jqlite wrapper as it might be changed later
// by a directive with templateUrl when its template arrives.
block = {
clone: clone
};
$animate.enter(clone, $element.parent(), $element);
});
}
} else {
if (previousElements) {
previousElements.remove();
previousElements = null;
}
if (childScope) {
childScope.$destroy();
childScope = null;
}
if (block) {
previousElements = getBlockNodes(block.clone);
$animate.leave(previousElements).then(function() {
previousElements = null;
});
block = null;
}
}
});
}
Now, it is my understanding that when you register a watcher in a directive/service/controller, you should capture its return and on the current scope's destroy method call it.
I would expect something like this (cut down for brevity):-
var removeWatcher = $scope.$watch($attr.ngIf, function ngIfWatchAction(value) {
...
}
$scope.$on('destroy', function destroy() { removeWatcher() });
I don't know if this is something I misunderstood or something born out ng-if being a special case born out of it being transclude: 'element'.