question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

how to initilize the module with variable apiKey

See original GitHub issue

I have the google api key in a singleton configuration service (a way to share the app configuration accross all the application).

How can I pass the apiKey variable the module inside @NgModule?

`@NgModule({

imports: [ BrowserModule, CommonModule, FormsModule, AgmCoreModule.forRoot({ apiKey: ‘YOUR_KEY’ // <-- I cannot use a constant!!! How can I pass a variable? }) ], providers: [], declarations: [ AppComponent ], bootstrap: [ AppComponent ] })`

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:5
  • Comments:12

github_iconTop GitHub Comments

31reactions
jor-ritcommented, Feb 3, 2017

Depends on what kind of build tool you are using (e.g. angular-cli or custom webpack or …) and where the variable should come from. This can be a little tricky. Specially when you also want to use AOT compiling, which prevents too much dynamic code inside the @NgModule.

The solution I currently implemented is overriding the ‘lazy config provider’ in my main app.module.

In the module where I actually use the map, I use an empty config in the forRoot:

AgmCoreModule.forRoot()

In my app.module.ts: import the injection token:

import { LAZY_MAPS_API_CONFIG }
  from 'angular2-google-maps/core/services';

Add to providers in @NgModule:

providers: [
    {provide: LAZY_MAPS_API_CONFIG, useClass: GoogleMapsConfig}
  ],

And implement that ‘GoogleMapsConfig’ class, which should implement the LazyMapsAPILoaderConfigLiteral interface (from ’ from ‘angular2-google-maps/core/services’).

@Injectable()
class GoogleMapsConfig {
  apiKey: string;
...
  constructor() {
    apiKey = getMyApiKeyFromSomewhere()
...
  }
}

In that injectable I can inject other services and read the config from somewhere. (e.g. if you use angular-cli you can import environments there). Or maybe read out the domain from the browser… or call an serverside API…

11reactions
bradrustcommented, Jul 27, 2017

@jorrit-wehelp, thank you for your guidance. I am including below what is working for me…

import {Injectable} from "@angular/core";
import {LazyMapsAPILoaderConfigLiteral} from "@agm/core";
import {Config} from "../providers/config";

@Injectable()
export class MapsConfig implements LazyMapsAPILoaderConfigLiteral{
  public apiKey: string
  public libraries: string[]
  constructor(config: Config) {
    this.apiKey = config.get("MAP_API_JS_KEY")
    this.libraries = ['places']
    console.log("lazy map init with " + this.apiKey)
  }
}

in my main @ngmodule, I have this in providers…

    {
      provide: LAZY_MAPS_API_CONFIG,
      useClass: MapsConfig,
      deps: [Config]
    }

The reference to the Config class is a build-time webpack variable-replace which setups of a map of variables used in Dev or Prod modes.

Read more comments on GitHub >

github_iconTop Results From Across the Web

how to initilize the module with variable apiKey #882 - GitHub
I have the google api key in a singleton configuration service (a way to share the app configuration accross all the application).
Read more >
how to initilize the module with variable apiKey - Bountysource
I have the google api key in a singleton configuration service (a way to share the app configuration accross all the application).
Read more >
3.4 Hiding API Keys with Environment Variables (dotenv) and ...
In this lesson, we will address how to can hide an API key using environment variables and open source the code on GitHub....
Read more >
initialize (Python agent API) - New Relic Documentation
Python API: Initializes Python agent with a specified config file during the manual integration process.
Read more >
How to create module-wide variables in Python? [duplicate]
Here is what is going on. First, the only global variables Python really has are module-scoped variables. You cannot make a variable that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found