Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

179
Views
Use defined value in module provider in other Angular module

I would like use provider value defined in other module. Here is example:

app.module.ts

...

import { ThemeModule } from '../shared/modules/theme/theme.module';

...

@NgModule({

  declarations: [
    RootComponent,
    LoginScreenComponent,
  ],

  imports: [
    BrowserModule.withServerTransition({ appId: 'serverApp' }),
    BrowserAnimationsModule,
    AppRoutingModule,
    ConfigModule,
    ThemeModule,
    ....
  ],

  providers: [
    ...
    { provider: "THEME_NAME", useValue: "VALUE" },
  ],
 
  bootstrap: [RootComponent]
})
export class MDMToolModule {}

theme.module.ts

import { NgModule } from '@angular/core';
import { ThemeService } from './services/ThemeService';

@NgModule({
    imports: [],
    declarations: [],
    providers: [
        {provide: "ThemeService", useFactory: (THEME_NAME) => {
            return new ThemeService(THEME_NAME)
        }},
    ],
    exports: []
})
export class ThemeModule {}

Is there possibility to pass VALUE defied not in module like above example (THEME NAME)?

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

if you're providing value in root module then it will be available to all other modules too so you can then simply ask for that value using Inject decorator like this:

@Injectable()
export class ThemeService {
  constructor(@Inject("THEME_NAME") theme: string) {}
}

otherwise you can import your MDMToolModule inside ThemeModule though judging by the code you provided I'm assuming this MDMToolModule is your root module, you can also use Injection Token to avoid using those magic strings like this:

const ThemeName = new InjectionToken("token to inject theme name");

and then use it inside theme.module.ts

@NgModule({
  providers: [
    { provide: ThemeName, useValue: "DarkKnight" },
    ThemeService
  ]
})
export class ThemeModule {}

theme.service.ts

@Injectable()
export class ThemeService {
  constructor(@Inject(ThemeName) theme: string) {}
}
about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!