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.
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;
});
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With