ngOnDestroy not called for injectables created via factory providers provided at component level #28738
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Duplicate of #14821, but possibly better to use new one for tracking. |
I still have this problem in Angular 8.0.0-rc.4, which probably means I'm doing something subtle wrong:
In this case the service is never destroyed when the back button is pressed, but the component is correctly destroyed.
In the root Module's providers array, but this made no difference. |
@Ghostbird ngOnDestroy from a service depends upon where it is provided. Here are you providing the service as singleton with: providedIn: 'root', so service will be destroyed (ngOnDestroy called) when the application is destroyed (theoretically). If you want this service to be created and destroyed with the component, you need to provide it at component level, like:
|
@llorenspujol After re-checking my code I recall that I thought that this construction:
Would provide the factory |
I am also suffering from this issue. A solution for the factory provider would be to expose an alternative interface. {
provide: FooService,
useFactory(
depService: DepService,
) {
const value = new SpecificFooService(
depService,
);
return {
value,
onDestroy: () => value.ngOnDestroy(),
};
}, deps: [
DepService,
],
}, |
Any update on this? |
No news? Is this still a problem in Angular 9 ? |
probably not ideal but I'm accomplishing this by calling |
I do the same, but it is sooooo easy to forget it, or that other member of
the team (or new member) does not know it has to be done.
El jue., 5 de noviembre de 2020 17:37, Bradley Cummins <
notifications@github.com> escribió:
… probably not ideal but I'm accomplishing this by calling
this.Service.ngOnDestroy(); on the Component.ngOnDestroy() that
introduced the Service and it's working.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#28738 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABJ4MGMLLKKDIKGNXCTBUTTSOLIFLANCNFSM4GXQY7YQ>
.
|
… and are provided at component level (angular#28738) Fix ngOnDestroy to be called on useFactory providers that implement it and are provided at component level. Fix applies to both ViewEngine and
Any updates on this? This was little strange to figure out at first (as normal services without token have their ngOnDestroy called) and one can hit issues if using stateful component level services. |
Still an issue... |
Is this much easier to fix now that we have |
Still an issue with Angular 18: But this does seem to work on a service created by a factory:
|
I confirm that this issue is still happening in Angular 19. What a deja vu I just had after 6 years of opening the issue😆 |
🐞 bug report
Affected Package
@angular/core
Is this a regression?
Don't know
Description
ngOnDestroy is not called for Services provided via factory function into component level.
🔬 Minimal Reproduction
I forked the Angular repo and created a Merge Request including 2 tests that both should pass. Of course, one test don't pass, that is the ones that we provide the service using the factory function.
EDIT: I also have added the code to make the tests pass, although I am not in context at all, an maybe I am missing something in the path
Here is the MR: #28737
🌍 Your Environment
Angular Version: 8.0.0-beta.3
The text was updated successfully, but these errors were encountered: