I have a problem due to my config data being loaded at runtime and AnuglarFire2 wanting the data in the module declaration. I can access the data via direct injection, but I cannot figure out how to get the data to AngularFireModule in my module file.
Having the data loaded at runtime is the preferred way to get configuration data to the app. So, I cannot change this process. Therefore, I need to come up with a solution to the problem.
AngualrFire2 Setup Page https://github.com/angular/angularfire2/blob/master/docs/1-install-and-setup.md
Here are my files:
** Config Data Loaded (main.ts) **
function processConfigData(config, env) {
platformBrowserDynamic([
{provide: "appConfig", useValue: config},
{provide: "appEnv", useValue: env}
]).bootstrapModule(AppModule);
}
The config data is pulled from the server and is passed in to the app.
Then I get the data from Direct Injection.
** Config Service (app-config.service.ts)**
import { Inject, Injectable } from '@angular/core';
// Code came from:
https://gist.github.com/fernandohu/122e88c3bcd210bbe41c608c36306db9
@Injectable()
export class AppConfig {
constructor(@Inject("appConfig") private config, @Inject("appEnv") private env) {
}
/**
* Use to get the data found in the second file (config file)
*/
public getConfig(key: any) {
return this.config[key];
}
/**
* Use to get the data found in the first file (env file)
*/
public getEnv(key: any) {
return this.env[key];
}
At this point, I only know how to get the config data from direct injection. I cannot figure out how to get it into the module to configure AngularFire2. Here is the code AngularFire2 is recommending for setting it up:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { AngularFireModule } from 'angularfire2';
// Must export the config
export const firebaseConfig = {
apiKey: '<your-key>',
authDomain: '<your-project-authdomain>',
databaseURL: '<your-database-URL>',
storageBucket: '<your-storage-bucket>',
messagingSenderId: '<your-messaging-sender-id>'
};
@NgModule({
imports: [
BrowserModule,
AngularFireModule.initializeApp(firebaseConfig)
],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule {}
Any help solving this problem is greatly appreciated.
The call to
AngularFireModule.initializeApp
doesn't do anything magical, it simply returns the module along with some providers:Note that the Firebase config is provided using the
FirebaseUserConfig
token.Some of the tokens used in
AngularFireModule.initializeApp
are not public, so the best approach is to callAngularFireModule.initializeApp
and filter the config provider from the result. The config provider can then be made available in the call toplatformBrowserDynamic
using the information you have obtained dynamically: