0

i would like to know if it is possible to get a variable from my component and use it in my module. In this case i'm configuring a stripe checkout payment with ngx-stripe, this library requires the publish key from stripe like this:

 @NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    NgxStripeModule.forRoot('***your-stripe-publishable-key***'), //<-- here i would like to insert the variable that comes from component.
    LibraryModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})

This publish key is something that i have stored in my db for each user... so... how can i do it? thank you in advance.

2 Answers 2

1

If someone is trying to change the publishable key in ngx-stripe. there is a method in stripeService called .changeKey() that allows you to change the current key.

Sign up to request clarification or add additional context in comments.

Comments

0

You should check security issues, but i woul try something like this.

In your main.ts before initialization:

http.get('endpoint that retrieves key from db')
    .then( response => response.json())
     //use some global variable in a service to save the key.
    .then( response => myService.key = response.key)
     .then(() => plastformBrowserDynamic().bootstrapModule(AppModule))

Later you can use the global variable set on your service to set the NgxStripeModule.forRoot value.

2 Comments

Hi there, i've been trying some aproaches about how to change that key in app.module, i tried with enviroment variable even with global variables declared in typings.d.ts .... but didn't work... after reading deep in the ngx-stripe library, someone added a new method that let you change the current key. so calling stripeService.changeKey() you can pass the new key.
Great Sergio, glad you find the solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.