From 17a9e6d44d55aa048dcc72899974046e1da4c901 Mon Sep 17 00:00:00 2001 From: dustinwag Date: Mon, 28 Mar 2016 14:50:58 -0700 Subject: [PATCH] feat(confg): create a method to access the global app injector which contains references the bootstr This feature simply sets a reference to the injector on the IonicApp class, so it can be referenced by other components. This is sometimes necessary when injecting providers that depend on other providers. This issue is discussed here https://github.com/angular/angular/issues/4112#issuecomment-139381970, and Brandon Roberts' solution of an appInjector() method has been used to solve a variety of dependency injection conflicts. Unfortunately, it requires access to Angular's bootstrap() method, which Ionic handles in the @App decorator. This fix will create a reference to the appInjector(), so it can be references from within Ionic components. closes #5973 --- ionic/components/app/app.ts | 18 +++++++++++++++++- ionic/decorators/app.ts | 1 + 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/ionic/components/app/app.ts b/ionic/components/app/app.ts index 530d911247..011c8fc68c 100644 --- a/ionic/components/app/app.ts +++ b/ionic/components/app/app.ts @@ -1,4 +1,4 @@ -import {Injectable, NgZone} from 'angular2/core'; +import {Injectable, Injector, NgZone} from 'angular2/core'; import {Title} from 'angular2/platform/browser'; import {Config} from '../../config/config'; @@ -18,6 +18,7 @@ export class IonicApp { private _title: string = ''; private _titleSrv: Title = new Title(); private _isProd: boolean = false; + private _appInjector: Injector; constructor( private _config: Config, @@ -155,4 +156,19 @@ export class IonicApp { return this._cmps[id]; } + /** + * Set the global app injector that contains references to all of the instantiated providers + * @param injector + */ + setAppInjector(injector: Injector) { + this._appInjector = injector; + } + + /** + * Get an instance of the global app injector that contains references to all of the instantiated providers + * @returns {Injector} + */ + getAppInjector(): Injector { + return this._appInjector; + } } diff --git a/ionic/decorators/app.ts b/ionic/decorators/app.ts index 8f885d8e86..1b87843ec5 100644 --- a/ionic/decorators/app.ts +++ b/ionic/decorators/app.ts @@ -96,6 +96,7 @@ export function App(args: AppMetadata={}) { bootstrap(cls, providers).then(appRef => { appRef.injector.get(TapClick); let app: IonicApp = appRef.injector.get(IonicApp); + app.setAppInjector(appRef.injector); app.setProd(args.prodMode); });