Share session storage or local storage data between tabs in angular

1,621 views
Skip to first unread message

NANA DARKO

unread,
Mar 15, 2019, 2:03:07 PM3/15/19
to Angular and AngularJS discussion
How can I share session storage or local storage data between tabs in angular?

Harry Whitehouse

unread,
Mar 15, 2019, 2:13:31 PM3/15/19
to Angular and AngularJS discussion
You can read the local storage in one tab with code like this:

   this.scale = JSON.parse(localStorage.getItem('myScale'));

and save data in a tab with code like this:

   localStorage.setItem('myScale', JSON.stringify(this.scale));

These types of calls can be made in any component in your application.

If you have two components visible at the same time and want to share data between them, see this excellent tutorial:


HTH

Harry

NANA DARKO

unread,
Mar 15, 2019, 11:19:34 PM3/15/19
to ang...@googlegroups.com
This doesn't work because the session is stored for a different app in a different tab
.

--
You received this message because you are subscribed to the Google Groups "Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Harry Whitehouse

unread,
Mar 16, 2019, 7:15:11 PM3/16/19
to Angular and AngularJS discussion
Apologies, I misunderstood.  I thought you had tabs within a given Angular app  (something I'm working on now).  You are speaking of different browser tabs and two distinct apps.  

I think this other suggestion is probably not appropriate, but QzTray runs on the host OS and you can pass data to and from the file system on the host and your Angular app.  This requires that your user has QzTray running on their machine (although it is free) and it may not be available in mobile environments.  If this kind of setup would work for you and your customers, you could use a local file to share data between the two tabs.

NANA DARKO

unread,
Mar 21, 2019, 9:19:52 AM3/21/19
to ang...@googlegroups.com
I got a work around. I deployed the two applications with the same Url origin. That way the browser is fooled to believe that it is the same app running. i also used local storage instead of session storage. Thanks a lot @ Harry Whitehouse. 
Reply all
Reply to author
Forward
0 new messages