How to store global variable in Angular2, same with session of PHP?
instead use:
localStorage.setItem();
How to store global variable in Angular2, same with session of PHP?
instead use:
localStorage.setItem();
You could use a shared service for this. To have a unique instance of the service for the whole application, simply define it within the bootstrap
function:
bootstrap(AppComponent, [ SharedService ]);
This way, you will be able to set your global variable in it.
export class SomeComponent {
constructor(private service:SharedService) {
this.service.someProperty = 'some value';
}
}
Every elements (components, services) that inject this service will have access to this service.
If you want to be notifed when this field is updated, you could use an observable property as described below: