Skip to content

Instantly share code, notes, and snippets.

@LarsBergqvist
Last active February 3, 2020 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LarsBergqvist/20d1037480adbdf1617b1d8160efac7e to your computer and use it in GitHub Desktop.
Save LarsBergqvist/20d1037480adbdf1617b1d8160efac7e to your computer and use it in GitHub Desktop.
Loading google maps api with APP_INITIALIZER provider in Angular
export function appConfigInit(configService: AppConfigService,
googleMapService: GoogleMapsService, logging: LoggingService) {
// Load the configuration and init google api if maps should be used
return () => {
return new Promise((resolve) => {
configService.load().then(() => {
if (configService.useMap) {
logging.logInfo('Use map');
googleMapService.load(configService.googleAPIKey).then(() => {
resolve();
});
} else {
logging.logInfo('no map');
resolve();
}
});
});
};
}
@NgModule({
declarations: [
AppComponent,
LocationsListComponent,
AppBarComponent,
LocationDetailsComponent,
MapComponent,
NumberRangeValidator
],
providers: [
AppConfigService,
GoogleMapsService,
LoggingService,
{
provide: APP_INITIALIZER,
useFactory: appConfigInit,
multi: true,
deps: [AppConfigService, GoogleMapsService, LoggingService]
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment