Hiya! Newbie here, I was looking around for a problem this specific but I havent found anything… so here goes!
My ionic app is structured this way:
MainPage
SectorPage
When you select a Sector on the MainPage, it will show you SectorPage but with specific information. So if I click on “Water Sector” in MainPage it should change some things of SectorPage to visually act accordingly: a blue background color, for example.
So these colors are stored in the globals provider, which is initialized when the IntroPage is loaded at first, like this:
export class IntroPage {
constructor(public platform : Platform, ...){
platform.ready().then(() => {
this.globals.setVariables();
});
}
}
Then I clic WaterSector and it would go as follows:
HTML: <button (click)="openSector(0)">WaterSector</button>
TypeScript:
openSector(id) {
this.navCtrl.push('SectorPage', {id:id});
}
Then SectionPage has NavParams on its constructor, and the id number passes just fine:
export class SectionPage {
constructor(public navCtrl: NavController, public navParams: NavParams,
public globals : GlobalsProvider) {
console.log(this.navParams.data); [returns 0 - OK]
}
}
Then on GlobalsProvider (EDIT: rest of the code on reply below, to avoid filling too much on this post)…
getColor (id : number){
console.log(id);
return this.sectionColors[id].color;
}
At this step, I get an error that says:
Error: Uncaught (in promise): TypeError: Cannot read property ‘color’ of undefined
TypeError: Cannot read property ‘color’ of undefined
sectionColors does exist! It’s an array where each spot has an id and a color but seemingly it lost its data in the transition between one page to another, even though all this happens in a provider!
What am I doing wrong here?
Any tips will be deeply appreciated!
Thanks in advance!