1.5 Feature: Backport ngOnDestroy (as $onDestroy for example) exactly as ngOnInit ($onInit) #14020
Description
In new 1.5.0 release is possible to use $onInit hook which is similar to ngOnInit
from Angular 2 lifecycle hooks. It will help with keeping controller's constructors clean, with initialization logic moved to the $onInit
method.
I think worth consideration is backport also ngOnDestroy
, as $onDestroy
for example.
Use Case:
In recent project in my company on of the business rules is to send request to device every second (which we have done with timeouts). We try to stay away as possible from injecting $scope
to controllers, but timeouts needs to be cleared when user opens new view or hits back button on mobile device etc... So we solved this with $scope.$on
:
$scope.$on('$destroy', function () {
viewModel.removeIntervals();
});
And it's only one direct usage of $scope
at this moment. If we have $onDestroy
hook, code would be like:
viewModel.$onDestroy = function () {
viewModel.removeIntervals();
});
And we can get rid off $scope
injection completely. Also, it let us keep code closer to Angular 2 syntax.
Thank you in advance for looking on this post.
Best regards,
Radek.