configuration management in Angular 2 : Some of So many ways

Mansura H.
Web Application Development
1 min readNov 28, 2016

Setting up global configuration in application is a must requirement for almost every application that I have developed using ng2 these days. There are a number of ways to do that. For example, Reading configuration files in Angular 2.

I have tried some other ways which costs me 2–3 lines.

Using Require

  1. Create a config.json file in config folder

2. Add config properties in config.json

{
"env":"development",
"apiurl":"http://localhost:9080/api/v1/"
}

3. Use require to access the config.json file

var config = require('./../../config/config.json');

@Injectable()
export class LandingService {
private url = config.apiurl;
constructor() {
console.log("config>>"+JSON.stringify(this.url));
}
....
}

Using import .ts file

  1. Create configuration.ts file as following

2. Add configuration properties as following

export const configuration = {
apiurl: "http://localhost:9080/api/v1/",
apikey:'dskfhi32t45b'
};

3. Import configuration and use properties as following

import  {configuration} from './../../config/configuration';

@Injectable()
export class LandingService {
private apiurl = configuration.apiurl;
constructor() {
console.log("api_url>>"+JSON.stringify(this.apiurl));
}
....
}

--

--

Mansura H.
Web Application Development

Platform Architect @ IBM; Author of Hybrid Cloud Infrastructure and Operations Explained ..