# How to change value of global variable on any files in Ionic?

**URL:** <https://forum.ionicframework.com/t/how-to-change-value-of-global-variable-on-any-files-in-ionic/103294>\
**Category:** ionic-v3\
**Created:** [August 26, 2017, 11:27am UTC](https://forum.ionicframework.com/t/how-to-change-value-of-global-variable-on-any-files-in-ionic/103294 "2017-08-26T11:27:06Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dlim](https://avatars.discourse-cdn.com/v4/letter/d/54ee81/32.png) [@dlim](https://forum.ionicframework.com/u/dlim)\
**Post date:** [August 26, 2017, 11:27am UTC](https://forum.ionicframework.com/t/how-to-change-value-of-global-variable-on-any-files-in-ionic/103294/1 "2017-08-26T11:27:06Z")

</div>

Hi,

I’m new to Ionic/typescript. Would like to know how to have a global variable that can be changed/retrieved on any files on Ionic app. Tried different methods but it doesn’t work.

Basically this is to set/get the value of a refresh token from oauth2. The value needs to be overwritten and retrieved per each API call.

What’s the easiest way to implement this? Please help.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [August 26, 2017, 9:27pm UTC](https://forum.ionicframework.com/t/how-to-change-value-of-global-variable-on-any-files-in-ionic/103294/2 "2017-08-26T21:27:50Z")

</div>

[Read about service providers](https://angular.io/tutorial/toh-pt4).

---

<div class="post-metadata">

**Author:** ![dlim](https://avatars.discourse-cdn.com/v4/letter/d/54ee81/32.png) [@dlim](https://forum.ionicframework.com/u/dlim)\
**Post date:** [August 27, 2017, 11:35am UTC](https://forum.ionicframework.com/t/how-to-change-value-of-global-variable-on-any-files-in-ionic/103294/3 "2017-08-27T11:35:58Z")

</div>

Thanks for your response. Learned that it’s easier to implement this via localStorage.

Thanks!

---

<div class="post-metadata">

**Author:** ![AaronSterling](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@AaronSterling](https://forum.ionicframework.com/u/AaronSterling)\
**Post date:** [August 27, 2017, 11:49am UTC](https://forum.ionicframework.com/t/how-to-change-value-of-global-variable-on-any-files-in-ionic/103294/4 "2017-08-27T11:49:24Z")

</div>

Depends on what you mean by easier. It’s slower and uses more overhead than defining a service provider. But it’s only a couple lines of code because the library does the work.

---

<div class="post-metadata">

**Author:** ![rashnk](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rashnk/32/109109_2.png) [@rashnk](https://forum.ionicframework.com/u/rashnk)\
**Post date:** [August 27, 2017, 12:57pm UTC](https://forum.ionicframework.com/t/how-to-change-value-of-global-variable-on-any-files-in-ionic/103294/5 "2017-08-27T12:57:19Z")

</div>

**This is super easy**

_ **Create a provider first.** _`ionic g provider global`

Declare your variables in new Provider

```auto
import { Injectable } from '@angular/core';
/*
  Generated class for the GlobalProvider provider.
*/
@Injectable()
export class GlobalProvider {

  public myGlobalVar: string;

```

_ **Use this in other .ts files like below** _

```auto
import { GlobalProvider } from "../../providers/global/global";

constructor( public global: GlobalProvider) {
....

myfunction1(){
   this.global.myGlobalVar="setting global val"
}

myfunction2(){
   alert( this.global.myGlobalVar)
}

```

**_use in template file like below_**

```auto
<ion-content>
<h1>{{ global.myGlobalVar }} </h1>

```

_ **Dont forget this to set in app.module.ts** _

```auto
providers: [
   // StatusBar,
  // SplashScreen,
   // InAppBrowser,
   // SocialSharing,
   // AdMobFree,
    //DateAndTime,
    { provide: ErrorHandler, useClass: IonicErrorHandler },
  // FeedService,
    GlobalProvider
  ]

```

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [August 27, 2017, 8:15pm UTC](https://forum.ionicframework.com/t/how-to-change-value-of-global-variable-on-any-files-in-ionic/103294/6 "2017-08-27T20:15:03Z")

</div>

> [@dlim](#):
>
> Learned that it’s easier to implement this via localStorage.

Storage of any sort should only be used for things you want to persist across app restarts. Simply sharing data in a single run across pages is a bad fit for it.

---

<div class="post-metadata">

**Author:** ![mixian](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@mixian](https://forum.ionicframework.com/u/mixian)\
**Post date:** [October 18, 2018, 2:01pm UTC](https://forum.ionicframework.com/t/how-to-change-value-of-global-variable-on-any-files-in-ionic/103294/7 "2018-10-18T14:01:18Z")

</div>

Nice Solution! Great!

---

<div class="post-metadata">

**Author:** ![hiarly](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/hiarly/32/128025_2.png) [@hiarly](https://forum.ionicframework.com/u/hiarly)\
**Post date:** [December 5, 2019, 12:54pm UTC](https://forum.ionicframework.com/t/how-to-change-value-of-global-variable-on-any-files-in-ionic/103294/8 "2019-12-05T12:54:15Z")

</div>

Show. Funcionou perfeitamente! Obrigado rashnk
