Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 bootstrap process

I want to make an AJAX request before calling the bootstrap method in Angular 2 to bootstrap the application on page load.

Are there any hooks I can listen for to do this using Angular? I know I could wrap it in a vanilla javascript ajax request but I thought there maybe a standard way to do this with Angular 2. I need to get a configuration file from a server before I spin up the Angular 2 App.

like image 917
HomeBrew Avatar asked Jun 21 '26 11:06

HomeBrew


1 Answers

As far as I know Angular doesn't provide a special hook for this use case.
A simple "workaround" is to just wrap the content of the template of the root component with an ngIf:

@Component({
  selector: 'my-app',
  template: `
<div *ngIf="_config">
  ...
</div>`
...
)}
export class MyApp {
  private _config;
  constructor(configService:ConfigService) {
    configService.change.subscribe(value) => {
      this.config = value;
    });
  }
}
like image 59
Günter Zöchbauer Avatar answered Jun 24 '26 06:06

Günter Zöchbauer