diff --git a/src/config.module.ts b/src/config.module.ts index 475eb48..831455d 100644 --- a/src/config.module.ts +++ b/src/config.module.ts @@ -1,19 +1,16 @@ -import { Injectable, NgModule, ModuleWithProviders, APP_INITIALIZER, SkipSelf, Optional } from '@angular/core'; +import { NgModule, ModuleWithProviders, APP_INITIALIZER, SkipSelf, Optional } from '@angular/core'; import { CommonModule } from '@angular/common'; import { HttpClientModule } from '@angular/common/http'; import { ConfigService } from './config.service'; import { EnvConfig } from './env-config'; -let _env: EnvConfig; -export function ConfigFactory(config: ConfigService) { - const res = () => config.load(_env); +export function ConfigFactory(config: ConfigService, env: EnvConfig) { + const res = () => config.load(env); return res; } - - @NgModule({ imports: [ CommonModule, @@ -24,7 +21,7 @@ export function ConfigFactory(config: ConfigService) { { provide: APP_INITIALIZER, useFactory: ConfigFactory, - deps: [ConfigService], + deps: [ConfigService, EnvConfig], multi: true } ], @@ -32,7 +29,7 @@ export function ConfigFactory(config: ConfigService) { }) export class ConfigModule { - constructor( @Optional() @SkipSelf() parentModule: ConfigModule) { + constructor(@Optional() @SkipSelf() parentModule: ConfigModule) { if (parentModule) { throw new Error( 'ConfigModule is already loaded. Import it in the AppModule only'); @@ -40,9 +37,11 @@ export class ConfigModule { } static forRoot(env: EnvConfig): ModuleWithProviders { - _env = env; return { - ngModule: ConfigModule + ngModule: ConfigModule, + providers: [ + { provide: EnvConfig, useValue: env } + ] }; } } diff --git a/src/config.service.ts b/src/config.service.ts index 68df78f..3087662 100644 --- a/src/config.service.ts +++ b/src/config.service.ts @@ -19,7 +19,6 @@ export class ConfigService { load(env: EnvConfig) { return new Promise((resolve, reject) => { - console.log(this._env) this._env = env.state; this._fallbackDev = env.fallbackDev || false; diff --git a/src/env-config.ts b/src/env-config.ts index d4ca250..983cab3 100644 --- a/src/env-config.ts +++ b/src/env-config.ts @@ -1,5 +1,5 @@ -export interface EnvConfig { +export class EnvConfig { state: string; fallbackDev?: boolean; } \ No newline at end of file