Skip to content

Commit

Permalink
feat(confg): create a method to access the global app injector which …
Browse files Browse the repository at this point in the history
…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
angular/angular#4112 (comment), 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
  • Loading branch information
dustinwag committed Mar 28, 2016
1 parent ba6f92b commit 17a9e6d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
18 changes: 17 additions & 1 deletion ionic/components/app/app.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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,
Expand Down Expand Up @@ -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;
}
}
1 change: 1 addition & 0 deletions ionic/decorators/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down

0 comments on commit 17a9e6d

Please sign in to comment.