Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

182
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda