With the help of Hien Ngyuen's answer, I did this:
Added following service:
config.service.ts
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { Injectable } from '@angular/core';
import { HttpService } from './http.service';
import { ConstantService } from './constant.service';
@Injectable({
providedIn: 'root'
})
export class ConfigService {
constructor(
private httpService: HttpService,
private constantService: ConstantService
) { }
/*
* Load configuration from env.json
*/
loadConfig(): Observable<any> {
let url = './assets/env.json';
let requestObject = {
API_URL: url,
REQUEST_METHOD: this.constantService.REQUEST_METHOD_GET
};
return this.httpService.sendRequest(requestObject).pipe(map(this.extractData));
}
/*
* Extract data from response
*/
private extractData(res: Response) {
let body = res;
return body || {};
}
}
http.service.ts
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { ConstantService } from './constant.service';
const httpOptions = {
withCredentials: true,
headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};
@Injectable({
providedIn: 'root'
})
export class HttpService {
constructor(
private httpClient: HttpClient,
private constantService: ConstantService
) { }
sendRequest(requestData) {
if (requestData.REQUEST_METHOD == this.constantService.REQUEST_METHOD_GET) {
return this.httpClient.get(url, httpOptions);
} else if (requestData.REQUEST_METHOD == this.constantService.REQUEST_METHOD_POST) {
return this.httpClient.post(url, requestData.BODY, httpOptions);
} else if (requestData.REQUEST_METHOD == this.constantService.REQUEST_METHOD_PUT) {
return this.httpClient.put(url, requestData.BODY, httpOptions);
} else if (requestData.REQUEST_METHOD == this.constantService.REQUEST_METHOD_DELETE) {
return this.httpClient.delete(url, httpOptions);
}
}
}
Next, after ng build I added assets\env.json and I was able to change and read values from env.json.