0.0.3 • Published 6 years ago

ng-config-by-json v0.0.3

Weekly downloads
1
License
MIT
Repository
-
Last release
6 years ago

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

1) In assets, create JSON config files (one for development: config.json, and one for production: config.prod.json).

```json
{ "name": "Adam" }
```

2) Set the config file to use in environments (for environments.ts, set the property config to the path for config.json see example below). Do the same for environments.prod.ts.

```javascript
export const environment = {
    production: false,
    config: { configURL: 'assets/config/config.json' }
};
```

3) Create a custom options class that extends ConfigOptions:

```javascript
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;
}
```

4) Add Module, Options, and custom Options to AppModule:

```javascript
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 { }
```

5) Inject ConfigService into your project components to access the config object:

```javascript
import { NgConfigService } from 'ng-config-by-json';

myname: string = '';

constructor(private _cfg: NgConfigService) {
    this.myname = _cfg.config.name;
}
```

and use directly in the template:

```html
<div>{{myname}}</div>
```

or

```html
<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.