Answer the question
In order to leave comments, you need to log in
Why does angularJS have $destroy?
Explain with examples for what purposes it is used:
scope.$on('$destroy', function() {})
Answer the question
In order to leave comments, you need to log in
Example 1
Let's provide a directive for the bootstrap tooltip.
In the link function we write elem.tooltip({ /* opts */);
Let's say we have a table with 10 rows - tr, via ngRepeat.
We pressed the button in one of the tds, the tooltip opened. While it hangs, the line disappears due to some condition, let's say filter. BINGO! tooltip will remain. To remove it, in the link function of the directive, you need to write something like this code:
scope.$on('$destroy', function() {
elem.tooltip('destroy');
}
function FooCtrl($rootScope) {
this.someMethod = function() {
// some code ...
if (someCondition) {
$rootScope.$emit('someEvent');
}
}
}
function BarCtrl($scope, $rootScope) {
var deregFn = $rootScope.$on('someEvent', function() {
// some code ..
};
$scope.$on('$destroy', function() {
deregFn();
});
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question