Description
I'm submitting a...
[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
Current behavior
If we provide a service with a ngOnDestroy
method as a type provider the service's ngOnDestroy
lifecycle hook is called. If however the same service is provided via a factory function, the ngOnDestroy
lifecycle hook is never called.
Expected behavior
The behavior should not differ if using a type provider or a factory provider.
Minimal reproduction of the problem with instructions
Simple Demo: https://stackblitz.com/edit/angular-3azkmn
If you comment in the type provider, the ngOnDestroy
hook is called (console.log is performed).
If you use the factory provider the ngOnDestroy
hook is not called
What is the motivation / use case for changing the behavior?
I have tried to destroy an angular application manually (by calling the NgModuleRef.destroy method) that contains an angular router. After the appliction is destroyed the routing events still fire. The cause for this is that the ngOnDestroy
hook is never called. Further this leads to a memory leak, as the application is not garbage collected.
Other issues might be that someone changes the provider type and is not aware of this limitation. Thus introducing subtle bugs into the appliction.
Solution
I do not exactly know where to look for a fix, but the nodeDef.flags
do not contain the OnDestroy
bit. So the cause of this bug is probably somwhere in the compiler, which I am not too familiar with.
Environment
Angular version: ^5.2.0
Browser:
- [x] Chrome (desktop) version XX
- [x] Chrome (Android) version XX
- [x] Chrome (iOS) version XX
- [x] Firefox version XX
- [x] Safari (desktop) version XX
- [x] Safari (iOS) version XX
- [x] IE version XX
- [x] Edge version XX