user15122827
user15122827

Reputation:

Declare and change global variable in Angular?

I develop an Angular app based on .NET Core and I want to create a global variable for countries by using a DropdownList so that the global value can be changed easily. Then access this global value (selected country) through all of the other pages. Normally I would use the following approach, but I have really no idea how should I modify the following approach so that the user can set the global filter value (selected country) at any time.

globals.ts:

export const version: string="22.2.2";

heroes.component.ts:

import * as myGlobals from 'globals';

export class HeroesComponent implements OnInit {
public heroes: Hero[];
public selectedHero: Hero;

// Here we access the global var reference
public helloString: string="hello " + myGlobals.version + " there";

     ...

    }
}

However, I have no idea how can I set this global value by using this approach or is there any approach for this. Is there any proper way for this?

Shall I add a dropdownlist to globals.ts and update the version whenever user select the version on this dropdownlist?

Is using a service with BehaviourSubject the best option for fixing this issue?

Upvotes: 1

Views: 7637

Answers (1)

Mahesh T.
Mahesh T.

Reputation: 61

You can use html5 storage its allow up to 5mb

set value : localStorage.setItem(keyname, value)

get value : localStorage.getValue(keyname, value)

https://www.w3schools.com/jsref/met_storage_setitem.asp

Upvotes: 1

Related Questions