Page 1 of 1

IONIC 5 - global variables

Posted: Wed May 05, 2021 2:34 pm
by AndyP
Hi all,

I'm using the menu panel on the app page and have added buttons to that menu.

Due to some constraints I have for the app, I will be using a single page for all, but need to be able to show/hide various DIVs on the main page when clicking buttons from the menu.

Basically, on the menu, a button would simply set a boolean to true (whether by calling a routine or directly setting).
On the main page, I have a DIV that has a *ngif parameter with the boolean listed above, but I cannot get it to work as the boolean can't be seen by both pages...

This worked in IONIC1 when I just created the boolean in the *index page, anyone know how to achieve this please?

Re: IONIC 5 - global variables

Posted: Thu May 13, 2021 12:27 pm
by Galyna Abramovych
Hello,

You can create a global variable and use it where needed using our helper Apperyio.data.getVariable("myBoolVar1").
We also have a snippet for adding the global variable: https://docs.appery.io/docs/ionic-4-sni ... t-variable.
Check the attached for more details.

Re: IONIC 5 - global variables

Posted: Tue May 18, 2021 2:24 pm
by AndyP
Perfect, many thanks for this :-)

Re: IONIC 5 - global variables

Posted: Thu May 20, 2021 9:27 am
by AndyP
One question related to this....

How would I display a global variable in a text field.....

I tried to use {{Apperyio.data.getVariable("myBoolVar1")}}

but it errored.....

I really didn't want to have a timer that pushes the value of a global into a local variable......

Re: IONIC 5 - global variables

Posted: Sun May 23, 2021 5:21 am
by Serhii Kulibaba
Hello,

Please read this value to the local variable on this page and use that variable on the label.

But there is another way to make it work:

Please create a simple Angular service (Create new -> TypeScript -> Angular service) it has some examples.
After that you can add this service on any page you need (in the tab code->Internal includes) and define a variable, based on that include.
It will allow you to call some general code on any page you want, no needed to rewrite it from local variable to the localStorage and backward