Joseph hooper
Joseph hooper

Reputation: 1047

Variable persistence in Angular Services

I would like to get some clarification on how global variables work in terms of services when using angular. I'm trying to find a clean way to store a user's generic data throughout my app so I can pull and update it whenever a page needs to.

I tried storing the currentUser variable in my User service, and it doesn't seem like that user variable stays put throughout a session. When I log in I store the variable, and I'm navigated to the home page where on initialization it pulls that stored user from my User Service and logs it. The only problem is when I reload the home page it throws a "cannot read property of undefined" error, telling me that the stored user is disappearing on the page reload.

Any idea why this may be happening?

Upvotes: 1

Views: 1502

Answers (1)

Joseph hooper
Joseph hooper

Reputation: 1047

Even though you can use Angular Services to hold variables you need to access and pass along through several components, upon refresh or exit of the application all of those variables are reset. So in addition to storing the variable in the services you also need to store the variable in some sort of storage so you can recall it.

Upvotes: 2

Related Questions