Sharing objects between components, pages


Sorry if i am asking too much of question. Still learning ionic 2 with typescript and some parts i am not sure whether I’m doing right.

With ionic 1, we had $rootScope or even there was a way to access parent objects using $parent.
with ionic 2, what is the best practice to share objects between pages.

In my app.ts, i have a variable “sharedObject” and potentially want to update that object from other Page class.

Below is just an sample code

class MyAppClass {

   constructor() {

   updateSharedObject() {
       // need to update sharedObject which is being used in home.ts

In my page class (home.ts),

export class HomePage {

   sharedObject: any;

What’s the best way to do this?

How to Access Data/Variable from One Page to Another

using Events subscribe/publish method seems a way to do it, but wasn’t working as expected.

How to Access Data/Variable from One Page to Another

I would say “as infrequently as possible”.

Generally, this sort of thing introduces too much coupling between parts of the app, resulting in unmaintainable and difficult to test spaghetti. That being said,

@App ({
  providers: [ThingyService]
export class ThingyService {
  private _thingy: Thingy;

  getThingy(): Thingy {
    return this._thingy;

  setThingy(thingy:Thingy) {
    this._thingy = thingy;
export class Page1 {
  constructor(private _thingyService:ThingyService) {

  updateThingy() {
    let thingy = this.acquireNewThingySomehow();
  template: `looking at {{thingy().foo}}`
export class Page2 {
  constructor(private _thingyService:ThingyService) {

  thingy():Thingy {
    return this._thingyService.getThingy();

But setThingy() in Page1 class won’t update the object in realtime in Page2 wouldn’t it? @rapropos


Yes, it would, because there isn’t really a “thingy object” in Page2, there’s a function that gets called over and over again. Yes, there are alternate ways using events or observables to keep various different copies of the same object synchronized, but I think this is the simplest.


i can’t understand how it’s work
case 1)
i have nav menuwhich is shared with all page but when i am login and go to dashboard after login so i can append new list which is log out.

my nav menu bar before login is
1.sign up
2.sign in details

my nav menu bar after login is details
3.log out

i’ creating global class for global variable
export class Global{
public loginState:boolean = false;

i have app.component.ts include

import { Global } from '../global/global';

  templateUrl: 'app.html'

export class MyApp {
   @ViewChild(Nav) nav: Nav;
   rootPage: any;
   is_login: boolean;
    //rootPage = loginPage;
    // Check if the user has already login
  constructor(public platform: Platform, public storage: Storage, public menu:MenuController,public global:Global) {
 //'hasLogin', '');,'loggedInMenu');
    console.log('i m hest','hasLogin'));'hasLogin')
      .then((hasUserLogin) => {
        if (hasUserLogin) {
          this.rootPage = HomePage;
        } else {
          this.rootPage = loginPage;
      this.is_login =  global.loginState;

app.html include

   <ion-item >Loan Simulator</ion-item>
      <ion-item  (click)="stateChange('loginPage')"> Sign In</ion-item>
      <ion-item (click)="stateChange('signUp')"> Sign Up</ion-item>
      <ion-item> Contact Us</ion-item>
      <ion-item *ngIf="is_login" (click)="logOut()"> Log Out</ion-item>

home.ts include

import { Global } from '../../global/global';
    selector: 'home-page',
    templateUrl: 'home.html'
export class HomePage{
     @ViewChild(Nav) nav: Nav;
     is_login : boolean = true;
    constructor(public navCtr : NavController,public menu:MenuController,public global:Global){
          global.loginState = this.is_login;,'loggedInMenu');

is_login is not reflect please help me out


I can’t make any sense out of your code, so all I can say is that here is how I handle user state in apps like this.


HI! could you explain what is Thingy in your code ?