Skip to content
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

Multiple instances of services create #6344

Closed
the-dixie-flatline opened this issue Jan 8, 2016 · 9 comments
Closed

Multiple instances of services create #6344

the-dixie-flatline opened this issue Jan 8, 2016 · 9 comments

Comments

@the-dixie-flatline
Copy link

I'm running into an issue with Beta.0 where services are being instantiated twice (easily verified by putting a console.log() into the services constructor). In some cases, components are being issued different instances of the same service and creates the following problem.

  1. MenuComponent and ContentComponent both have StateService as a provider.
  2. ContentComponent subscribes to StateService.viewChanged (log viewChanged shows observers set)
  3. MenuComponent changes StateService.view (via property set).
  4. StateService.view.set saves view and calls viewChanged.next(_view) (log viewChanged shows no observers)
  5. Observer(s) aren't called (since there are no observers on this instance).

What's interesting is I have another service that follows the same pattern and while two instances are created, the same instance is always injected so everything works properly.

It does not seem to be dependent on the number of times a Service is listed as provider, it's always initialized twice.

@ericmdantas
Copy link
Contributor

Could you set up a plnkr, please?

@the-dixie-flatline
Copy link
Author

https://embed.plnkr.co/xnVaqaUjZptFntu0yZMD/

Creates three components (Header,Content,Footer) that all use TestService. Each component gets a different instance of TestService (which is actually slightly different than what I'm seeing locally but still demonstrates the general issue).

@the-dixie-flatline
Copy link
Author

After re-reading the doc's it's working as it's supposed to with Providers creating a new instance for each component. Apologies for the misunderstanding.

@zoechi
Copy link
Contributor

zoechi commented Jan 8, 2016

If you want a single instance, only add it once to providers in bootstrap(AppComponent, providers: [..., TestService]); but not at the components.

@ericmdantas
Copy link
Contributor

Yeah, if you really want an application-wise singleton, you could do:

bootstrap(MyApp, [MyService]);

And then do not use providers:

@Component/Directive({
   ...
   providers: [MyService] // this will give you new instance, 
                          // removing it will give you the same instance in bootstrap
})

@ericmdantas
Copy link
Contributor

Lol, beat me for 2 seconds 😆

@the-dixie-flatline
Copy link
Author

Appreciate the follow up.

@zoechi
Copy link
Contributor

zoechi commented Jan 8, 2016

@ericcarraway less characters in my post ;-)

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants