FarmMapsLib/projects/common/src/fm/shared/app.config.ts

34 lines
886 B
TypeScript
Raw Normal View History

2019-07-15 14:54:19 +00:00
import {Inject, Injectable} from '@angular/core';
2019-11-11 16:38:49 +00:00
import { Location } from '@angular/common';
2019-07-15 14:54:19 +00:00
import {HttpClient, HttpXhrBackend} from '@angular/common/http';
import {Observable} from 'rxjs';
@Injectable()
export class AppConfig {
private config: Object = null;
private httpClient: HttpClient;
2019-11-11 16:38:49 +00:00
constructor(xhrBackend: HttpXhrBackend,private location:Location) {
2019-07-15 14:54:19 +00:00
this.httpClient = new HttpClient(xhrBackend);
this.config = null;
}
public getConfig(key: any) {
if (!this.config.hasOwnProperty(key)) {
2019-11-06 08:08:42 +00:00
console.error(`Config key ${key} not set`);
2019-07-15 14:54:19 +00:00
}
return this.config[key];
}
public load(): Promise<any> {
2019-11-11 16:38:49 +00:00
return this.httpClient.get(this.location.prepareExternalUrl('/configuration.json'))
2019-07-15 14:54:19 +00:00
.toPromise()
.then(data => {
this.config = data;
//return data;
})
.catch(error => this.config = null);
};
}