2
votes

I am new to ionic, angular and typescript. I have a variable "ar: AR" that I need in all pages, so I thought it is a good idea to make it a global variable. I do it like this:

First I define:

@Injectable()
export class AR {

 public roll: FR;

 constructor() {
   this.roll = new FR("test");
 } 

 set_roll(_roll) {
    this.roll = _roll;
 }

 get_roll() {
    return this.roll;
 }

}

Then I add the class as a provider in app.module.ts.

import { AR } from ...
@NgModule({
  ...
 providers: [ AR, ...]
 })

In app.compontents.ts I define a function:

export class MyApp {
rootPage:any = HomePage;

constructor(public ar: AR) {

  platform.ready().then(() => {
    ...
  }

}

activate(roll){
    this.ar.set_roll(roll);
   }

}

and in the app.html I define a button which should set this variable "ar" to a new value which is stored in an array named "myArray":

<ion-content>
    <ion-list>
          <ion-item *ngFor="let i of myArray (click)="activate(i)">
   </ion-list>
</ion-content>

However, the value of the global variable does not change. It always stays the inital value "test" defined in the constructor of the AR class.

It should be printed in another page:

import { AR } from ...;


@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

    ar_name: string;

    constructor(..., public ar: AR) {
      this.ar_name = this.ar.get_roll().get_rollname();
    }

(get_rollname is a function of the AR class and simply returns a string.)

In the respective HTML:

 <ion-footer>
  <div> {{ar_name}} </div>
 </ion-footer>

What am I doing wrong?

Edit:

export class FR {

    private rollname: string; 

    constructor(rollname: string) {
      this.rollname = rollname
    }

    set_rollname(newName: string) {
       this.rollname = newName;
    }

    get_rollname() {
       return this.rollname;
    }

}
4
can you add the code where you are actually showing the value in html? which component is it? - Suraj Rao
Thank you, I added the respective part :) So what am I doing wrong? - refle
Are you able to console log the value? And you have a typo in app.html.. missed a " - Suraj Rao
Try without innerhtml - Suraj Rao
how can I do it without "innerhtml"?? how do i get the value? and i still have to figure out where the consol log is written :D - i am still a newbe - refle

4 Answers

1
votes

Another way might be to simple make the class a singleton.

class A {
    static instance = null;
    constructor() {
        if(A.instance) return A.instance;
        A.instance = this;
    }
}

I have not tried it with thie angular injectors but I am curious if it would work?

3
votes

Thank you all for your help. I actually solved the whole thing by putting all the data arrays and respective functions in a provider. Like this all data and methods are available to all pages. USE PROVIDERS - it makes everything so much easier and simpler.

2
votes

Injectors can be lazily loaded async and thus there are times where there might be more than 1 instance of the injectable beacuse there are more than one injector. See here and here.

Perhaps this applies to you.

0
votes

If you want to use a global variable, it would be best to work with ionic storage module.

See here: https://ionicframework.com/docs/storage/

If you rewrite yourself the AR provider, then not only can you access it globally (if you import he provider in one of your pages), but the saved data survives closing and opening the app.

Also notice that the get(key) method returns a promise (async). So you want to do something as

getRoll().then((data)=> {
 // your code
});

As you are a beginner, you might want to read about asynchronous code first.

PS: As I read from your answers above, you don't know how to console log. Just write console.log("") in your code and it will be put out in your terminal. I recommend watching the Ionic Crash Course on Youtube from the Ionic team.