For my App I need runtime configuration (docker architecture), so I cannot rely on Angular's environment mechanism. So I created my own config file that I read like this:
@Injectable()
export class ConfigService {
config;
constructor(private http: Http) {
this.loadConfig();
}
loadConfig() {
this.http.get('./../../../assets/config.json')
.map((res: Response) => res.json())
.catch((error: any) => Observable.throw(error.json().error || 'Server error'))
.subscribe(data => {
this.config = data;
})
}
}
This worked well until I tried to access my config during another component's nngOnInit(): config was undefined at the time. I realized that the observable had not returned.
- Is there a better approach to solve the runtime configurability ?
- How can I make sure the config has been loaded before I use it elsewhere ? I thought of solutions like a boolean "isLoaded", or even checking if "config" is undefined - but there must be a better way.
Any idea is welcome