Reputation: 930
I'm trying to set global variable in my LoginService
so I can inject it to my DriverService
. However, no matter how I implement the global variable, it's always either undefined
or empty string.
I made a plunker, so you can check it live: PLUNKER
I tried with setValue (this.login = value
) and getValue(return this.login
) things, now I tried with Subject and BehaviorSubject, but with no success.
How can I set my global variable in one service, so I can use it in another service?
Thanks
Upvotes: 2
Views: 3740
Reputation: 73377
What you have to remember with Services (Injectables in general), is that if you want the same instance of service in your app, you need to add them to providers
array in your NgModule
, which will make the same instance of service available for all components that are in that module. In this case you of course want exactly that!
Having providers
-array in each component, means that all your components will have their own instance of the service(s), so that means they are not sharing the same data at all.
So the only thing needed was to remove the providers
arrays from the components, and instead apply them in your NgModule
:)
Your fixed PLUNKER.
Upvotes: 4