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

193
Visualizações
How one service (not singleton) can be used by one directive and one component?

I have created a directive that is using component as its template. Both of them use the same service included in providers:

@Component({
  selector: 'my-component',
  providers: [MyService],
  ..
}

@Directive({
  selector: '[myDirective]',
  providers: [MyService]
})

Now here is the problem. They use the same service, but while one is changing the server, the directive that has been register to its change didn't got the change at all:

this.myService.dataUpdated.subscribe
      (
        (data: someObject) => {
          // Do something...
        }
      )

Seems to me they are not using the same service at all.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I don't see a reason why singleton should not be used for your scenario. It seems to me that singleton should be used on your case, that's why you don't see the changes at all.

Try to provide your service in your NgModule like this

 @NgModule({
  ...
  providers: [MyService]
  ...

Hope this helps.

over 4 years ago · Santiago Trujillo 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