Skip to content

Instantly share code, notes, and snippets.

@scriptmaster
Created May 9, 2018 19:22
Show Gist options
  • Select an option

  • Save scriptmaster/c5df9d5345daca003ba58437d3c257dc to your computer and use it in GitHub Desktop.

Select an option

Save scriptmaster/c5df9d5345daca003ba58437d3c257dc to your computer and use it in GitHub Desktop.

Revisions

  1. scriptmaster renamed this gist May 9, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. scriptmaster created this gist May 9, 2018.
    4 changes: 4 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    For modern Angular projects (version 2+), you need to maintain a config.json file per every environment: dev, staging or production.

    If you normally import or require this file in your environment.ts file(s) it would be merged as part of your bundle and may not be editable by admins who need to edit them in new clusters.

    23 changes: 23 additions & 0 deletions src__app__app.module.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    import { NgModule } from '@angular/core';
    import { BrowserModule } from '@angular/platform-browser';
    import { HttpClientModule } from '@angular/common/http';
    import { AppComponent } from './app.component';

    import ConfigService from '../services/config.service';


    @NgModule({
    declarations: [
    AppComponent
    ],
    imports: [
    BrowserModule,
    HttpClientModule
    ],
    providers: [
    ConfigService,
    ConfigService.getAppInitializer('/assets/config.json')
    ],
    bootstrap: [AppComponent]
    })
    export class AppModule { }
    34 changes: 34 additions & 0 deletions src__services__config.service.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    import { Injectable, APP_INITIALIZER } from "@angular/core";
    import { HttpClient } from "@angular/common/http";

    function getHostUrl() {
    return '//'+location.host;
    }

    @Injectable()
    export default class ConfigService {

    public config: any;

    constructor(private http: HttpClient) {
    }

    load(path: string) {
    return new Promise((resolve, reject) => {
    this.http.get(getHostUrl()+path)
    .subscribe(data => {
    this.config = data;
    resolve(true);
    })
    });
    }

    static getAppInitializer(path: string) {
    return {
    provide: APP_INITIALIZER,
    useFactory: (config: ConfigService) => () => config.load(path),
    deps: [ ConfigService ],
    multi: true
    };
    }
    }