Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.