I have a controller that loads a lot of data with an observable. If I navigate away from that route and the view is removed from the page how do I get informed of that in my controller so that I can stop the data load in the controller?
I basically want a deactivate function or clean up function that gets fired when the controller is no longer needed.
You can listen for the $destroy
event, and perform some clean-up/tear-down:
module.controller("SomeController", function($scope) {
$scope.$on("$destroy", function() {
// clean up here
});
});
Resources:
$on
: https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$on
$destroy
: https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$destroy
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With