angular-magic / ngx-gp-autocomplete

17 stars 4 forks source link

Use without Loader injection not possible #21

Open raz-mt opened 4 months ago

raz-mt commented 4 months ago

I believe my js is loading before my markup, and am using a

@NgModule({
  imports: [NgxGpAutocompleteModule, FormsModule],
  exports: [NgxGpAutocompleteModule],
  providers: [
    {
      provide: Loader,
      useValue: new Loader({
        apiKey: 'hi', //todo get from config
        libraries: ['places'],
      }),
    },
  ],
})
export class GoogleAddressModule {}

as suggested. The issue now being: I don't wish to use loader as there are a range of ways to load the api, which conveniently address my issue as stated above.

I get runtime error

  NullInjectorError: No provider for Loader!
  ...
    at NodeInjectorFactory.NgxGpAutocompleteDirective_Factory [as factory] (angular-magic-ngx-gp-autocomplete.mjs:21:103)

when your lib attempts to inject the dep. Could we make the injection optional so that I can utilize other options?

dancornilov commented 4 months ago

Hey, a loader is required to ensure that the library works as expected.

I understand that you want to make an API request to obtain the apiKey and then create your loader. In this case, try using Angular's APP_INITIALIZER hook to extract your apiKey. Then, create your loader and use it in your module.