0

I am using service to pass data between components, but when I open a page in new tab like this:

<a [routerLink]="['/preview ']" target="_blank" class="btn">Preview</a>

then the app refreshes and data from service is lost; but if I omit target="_blank" then data is being passed to the components. But I need to open the other page on a new tab upon clicking, how can I do that without losing the passed data? My code is similar to this PLUNK. Thanks in advance.

Jane
  • 283
  • 3
  • 5
  • 16

1 Answers1

0

Since Angular services instances are just 'usual' js objects behind the scene , you can't use them to pass data between tabs. I see two approaches in this case:

1) Store data in localStorage and access it from any new tab when app.component init

2) Pass data thru Url Parameter and configure specific angular route for that (look https://angular.io/guide/router)

  • Note that localStorage solution might potentially cause some race conditions (https://stackoverflow.com/questions/22001112/is-localstorage-thread-safe) – crollywood Mar 27 '18 at 10:16