0

I need to have a module that does instantiate another one with a custom variable like the following.

// app.module.ts
MyServiceModule.forRoot({
 custom: 'customVar'
})

Then within the myServiceModule I try to do the following

#NgModule({
  imports: [
    anotherServiceModule.forRoot({
      custom: // <-- The 'customVar' from MyServiceModule
    }) 
  ]
})
export class MyServiceModule {
  static forRoot(config: {custom: string}) {
    return {
      ngModule: MyServiceModule,
      providers: [
        {
           provide: MyServiceProvider,
           useValue: config,
        }
      ]
    }
  }
}

Can I somehow use the instance of my MyServiceProvider?

Edit Also, I'm not the owner of anotherServiceModule

Objectif

I did build an extended language service because the ngx-translate was missing some feature that my specific project did required.

I would like to initiate this library with prefix given in the forRoot()

Real life example

MyServiceModule.forRoot({
 custom: ['/assets/i18n']
})
@NgModule({
  imports: [
    TranslateModule.forRoot({
      loader: {
        provide: TranslateLoader,
        useFactory: HttpLoaderFactory,
        deps: [
          HttpClient, 
          custom //  <-- The 'customVar' from MyServiceModule
        ],
      },
    }),
  ]
}),
// ...
Raphaël Balet
  • 6,334
  • 6
  • 41
  • 78

1 Answers1

0

The services within AnotherServiceModule could require MyServiceProvider as a dependency. I'm guessing that by setting it's useValue in MyServiceModule.forRoot it would then be available as that value in the other services

I'm assuming AnotherServiceModule is something you have control over, and isn't a 3rd party module

If AnotherServiceModule is 3rd party, then import it in the AppModule - you already know what the custom variable is going to be, so apply it using AnotherServiceModule.forRoot(...)

Drenai
  • 11,315
  • 9
  • 48
  • 82
  • Hi & thx for your answer. I'm sadly not the owner of `AnotherServiceModule` I did update my question with more detailed information – Raphaël Balet Nov 01 '21 at 12:06