1

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.

  1. Is there a better approach to solve the runtime configurability ?
  2. 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

Tim
  • 3,910
  • 8
  • 45
  • 80
  • I solved my problem following this approach: https://stackoverflow.com/questions/41619443/how-to-call-an-rest-api-while-bootstrapping-angular-2-app – Tim Jul 24 '17 at 15:06

1 Answers1

0

This posttells you how to hook into Angular's APP_INITIALIZER constant to solve this problem.

pthalacker
  • 2,056
  • 3
  • 20
  • 37