Global Var

How I can create a Global variable or Global class?, with injectable service?

1 Like

Try this:

// global_service.ts
import {Injectable} from 'angular2/core';  
import {Http, Headers} from 'angular2/http';

@Injectable()
export class GlobalService {  
    constructor(private http: Http) {
    }
}
// app.ts
@App({
  providers: [GlobalService], // constructed once
  config: {} // http://ionicframework.com/docs/v2/api/config/Config/
})
class MyApp {...}
// page.ts
@Page({
})
export class SpecialsPage {
  // shared instance being injected
  constructor(private global: GlobalService) {
  }
}
1 Like

Link to formatted code.

global class

export class Global {
  public static globalVar1: string = 'lol.com';
  public static globalVar2: number = 42;
}

global variables

export const globalVar1: string = 'lol.com';
export const globalVar2: number = 42;

PS. It’s not service, and it’s not injectable.
PPS. TS

Mmmmm, it’s easier if you tell us what you want to do. Maybe this will help you http://www.gajotres.net/ionic-2-sharing-data-between-pagescomponents/