2

I am trying to convert my AngularJS application to Angular 2 Universal application (because of server-side rendering).

https://github.com/angular/universal-starter

Now I need to store global variables which could be achieved easily in normal Angular 2 applications(per below links).

Angular 2 global variable

Angular 2 - Whats the best way to store global variables like authentication token so all classes have access to them?

One thing you will have to do to achieve this in normal angular 2 App is to pass your global service to bootstrap.

I don't think you can do that in Angular 2 universal app unless I am missing something.

I want to store user data and use it across application. Just like Session data in asp.net. And plus this needs to done without making parent-child components

How can I store global variables in Angular 2 Universal App?

Thanks Fahad Mullaji

Community
  • 1
  • 1
Fahad Mullaji
  • 1,332
  • 5
  • 15
  • 30
  • Just create a top level service. Check my answer for method 1, it is exactly what you are looking for : http://stackoverflow.com/a/39031152/1810391 – John Siu Sep 18 '16 at 00:34
  • @JohnSiu that looks like what I really want to do. I will have to integrate this in my application and see how it works. Thanks – Fahad Mullaji Sep 18 '16 at 01:42
  • If it works for you, remember to upvote my answer, lol – John Siu Sep 18 '16 at 01:47
  • 1
    I don't think your solution is going to help me. I want to store user data and use it across application. Just like Session data in asp.net. And plus this needs to done without making parent-child components. – Fahad Mullaji Sep 19 '16 at 02:57
  • They don't have to be parents child. I can give you an example once I reach home. – John Siu Sep 19 '16 at 03:49
  • This would only work in an SPA environment. If you have an SSR site, then any values you might have assigned throughout your journey will get wiped when clicking through with href links. – Andrew Howard Dec 07 '22 at 14:24

1 Answers1

11

This is another way to use Method 1 of my answer at https://stackoverflow.com/a/39031152/1810391

To share a common data structure, just use a hard-coded index like global in all components.

globaldata.service.ts

import { Injectable } from '@angular/core';

interface ShareObj {
  [id: string]: any;
}

@Injectable()
export class GlobalDataService {
  shareObj: ShareObj = {};
}

app.module.ts(assume this is your root module)

import { GlobalDataService } from './globaldata.service';
//
// skip ..
//

@NgModule({
  //
  // skip ..
  //

  provider:[GlobalDataService]

})
export class AppModule {}

any.component.ts

code:
    import { GlobalDataService } from './globaldata.service';
    //
    // skip ..
    //

    constructor(private gd: GlobalDataService){
        // This can be string, array or object
        this.gd.shareObj['global']='data';
    }
Community
  • 1
  • 1
John Siu
  • 5,056
  • 2
  • 26
  • 47
  • In Angular 4: providers:[GlobalDataService] – Eman Jayme Sep 05 '17 at 08:03
  • I want to remove exisitng data in `logout()` and it gets cleared but constructor still holds the old value when I login with another account. What is could be the issue? – devN Dec 12 '17 at 05:41