IONIC 5 - global variables

Moderator: Support

AndyP
Posts: 10
Joined: Thu Jul 23, 2020 8:25 pm

IONIC 5 - global variables

Post 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?
Galyna Abramovych
Site Admin
Posts: 69
Joined: Tue Mar 22, 2016 6:03 pm

Re: IONIC 5 - global variables

Post 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.
Attachments
image_2021_05_13T12_18_29_815Z.png
image_2021_05_13T12_18_29_815Z.png (15.64 KiB) Viewed 1072 times
image_2021_05_13T12_15_03_523Z.png
image_2021_05_13T12_15_03_523Z.png (70.44 KiB) Viewed 1072 times
image_2021_05_13T12_14_10_755Z.png
image_2021_05_13T12_14_10_755Z.png (52.08 KiB) Viewed 1072 times
AndyP
Posts: 10
Joined: Thu Jul 23, 2020 8:25 pm

Re: IONIC 5 - global variables

Post by AndyP »

Perfect, many thanks for this :-)
AndyP
Posts: 10
Joined: Thu Jul 23, 2020 8:25 pm

Re: IONIC 5 - global variables

Post 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......
Serhii Kulibaba
Posts: 147
Joined: Tue Aug 27, 2013 1:47 pm

Re: IONIC 5 - global variables

Post 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
Post Reply