ng-config-by-json
v0.0.3
Published
An Angular library to easily load a JSON config file during initialization of the application.
Readme
ng-config-by-json
An Angular library to easily load a JSON config file during initialization of the application.
This project was generated with Angular CLI version 1.7.0.
How to use
In
assets, create JSON config files (one for development: config.json, and one for production: config.prod.json).{ "name": "Adam" }Set the config file to use in environments (for environments.ts, set the property
configto the path for config.json [see example below]). Do the same for environments.prod.ts.export const environment = { production: false, config: { configURL: 'assets/config/config.json' } };Create a custom options class that extends ConfigOptions:
import { ConfigOptions } from 'ng-config-by-json'; import { environment } from '../environments/environment' export class IQConfigOptions extends ConfigOptions { configURL: string = environment.config.configURL; appVersion: string = '0.0.1'; bustCache: boolean = false; }Add Module, Options, and custom Options to AppModule:
import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { FormsModule } from '@angular/forms'; import { MyConfigOptions } from './my-config-options'; import { NgConfigModule, ConfigOptions } from 'ng-config-by-json'; import { AppComponent } from './app.component'; @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, FormsModule, NgConfigModule.forRoot() ], providers: [ { provide: ConfigOptions, useClass: MyConfigOptions } ], bootstrap: [AppComponent] }) export class AppModule { }Inject ConfigService into your project components to access the config object:
import { NgConfigService } from 'ng-config-by-json'; myname: string = ''; constructor(private _cfg: NgConfigService) { this.myname = _cfg.config.name; }and use directly in the template:
<div>{{myname}}</div>or
<div>{{this._cfg.config.name}}</div>
ConfigOptions Attributes
configUrl
The location of the configuration file. Default: 'assets/config.json'
appVersion
The version of the application. To ensure client browsers are getting the latest version of your config file, increment this value when releasing your application. Default is empty and will not append a version to the config URL.
bustCache
If set to true, will append a random value to the end of the config URL. Default is false.
