Loading google maps api with APP_INITIALIZER provider in Angular
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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