There are several components. Root, and not how many pages. Moving to another page is done using

this.navCtrl.push(CityPage); 

I need from each page to memorize the selected item. How can I create a global variable for this?

I have only ideas, or add everything to the local repository. Or transfer to each component new data so

 this.navCtrl.push(CityPage, params[]); 

Are these solutions normal? And how can this be implemented through a global variable

  • This can be done through the service - Alexandr

1 answer 1

Option 1 - declare a variable, for example, in the app.module declare var params:any = []; but it's not very cool.

Option 2 - create as Alexander wrote, create a service model, make it @Injectable place in the providers module and add to components.

It all looks something like this:

  import { Injectable } from '@angular/core'; @Injectable() export class ParamsModel { private params:any = []; constructor() {} public setParams(param) { this.params.push(param); } public getParams() { return this.params; } } 

app.module

  import {ParamsModel} from "PATH/TO/FOLDER"; ... @NgModule({ imports: [ .... ], declarations: [ ... ], providers: [ ParamsModel ], ... }) 

Well, the component itself ...

  ... export class YourComponent{ constructor(private paramsModel:ParamsModel) { console.log(paramsModel.getParams()); } } 
  • CUSTOM_ELEMENTS_SCHEMA why? - Grundy
  • @Grundy oh, I'm sorry, I didn’t remove it from the project - Kostiantyn Okhotnyk