question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dependency injection creates new instance each time if used in router submodule

See original GitHub issue

I’m submitting a … (check one with “x”)

[X] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior My application is divided into modules. Router navigates between modules, each child modules has some providers and some components. I expect application to reuse services injected to components within one module. However, when I navigate, service constructor is called each time component is loaded.

RootModule:

		RouterModule.forRoot([
			{ path: '', component: EmptyComponent },
			{ path: 'module', loadChildren: () => SubModule },
		]) 

SubModule:

		RouterModule.forChild([
			{ path: 'test1', component: TestComponent1 },
			{ path: 'test2', component: TestComponent2 },
		])

...
	providers:
	[
		TestService
	],

Each time I navigate between /module/test1 and /module/test2 - TestService constructor is called

Expected behavior If test1 and test2 are defined as routes in RootModule, only one instance of TestService is created. I expect SubModule to inject the same instance of TestService if I navigate between its components.

If I move TestService provider to RootModule - works as expected

Minimal reproduction of the problem with instructions https://github.com/peku33/angular2-dependency-injection-failure

What is the motivation / use case for changing the behavior? Most of components of one of modules uses a service that opens and maintains WebSocket connection. Each time a service is spawned, new connection is opened. I definitely want to reuse the component and keep single connection open than close and reopen it every time user navigates between pages.

Please tell us about your environment: Everything is up to date, browser - Chrome

  • Angular version: 2.0.X Have ‘latest’ for almost everything in package.json @angular/core@2.2.0

  • Browser: [all | Chrome XX | Firefox XX | IE XX | Safari XX | Mobile Chrome XX | Android X.X Web Browser | iOS XX Safari | iOS XX UIWebView | iOS XX WKWebView ]

  • Language: [all | TypeScript X.X | ES6/7 | ES5]

  • Node (for AoT issues): node --version =

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:15
  • Comments:29 (4 by maintainers)

github_iconTop GitHub Comments

16reactions
Keksdroidcommented, Jan 8, 2018

I’m experiencing this issue as well. Is it still a present bug or is the problem on my end?

Angular 5.1.3

12reactions
peku33commented, Nov 15, 2016

@guojenman I know. But the problem is, that DI creates multiple objects for components inside THE SAME module

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why my service creates new instance of its dependency?
I've realised that it happens in my clients app when they have lazy loading for sub module, and importing my module there.. For...
Read more >
Providing dependencies in modules - Angular
A provider is an instruction to the Dependency Injection system on how to obtain a value for a dependency. Most of the time,...
Read more >
How to avoid Angular injectable instances duplication
How to avoid Angular injectable instances duplication. In this article we'll explore when and why Angular creates two instances of the same service...
Read more >
Using Dagger in multi-module apps - Android Developers
Now, LoginActivity can create an instance of LoginComponent and call the inject() method. Kotlin Java More. class ...
Read more >
Total Guide To Angular 6+ Dependency Injection - Medium
Every class has a special function called constructor which is called when we want to create an object (instance) of that class to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found