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

382
Visualizações
(Angular) Why would you want multiple instances of the same service?

I might be missing something, and people usually ask "How to" not "Why to" whenever I google it

But what's the point of actually creating multiple instances of the same service where each instance has the same functions, same variables?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

To hold state.

Imagine you have an application, which has multiple tabs. Let's say they display car information, so each tab has a different car. You provide service in "root tab" component, so instance of that service will be available to all sub-components/services within that that tab, and not outside. This way you write service that stores data of one car, which reduces service complexity.

Small example

@Injectable()
export class CarService {
  color = '';
}
@Component({
  ...
  providers: [CarService]
})
export class TabComponent {
  constructor(private car: CarService) {}
  getColor() { return this.car.color }
}

This way you can have

TabComponent
  getColor() -> red
TabComponent
  getColor() -> blue
TabComponent
  getColor() -> black
about 4 years ago · Juan Pablo Isaza 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