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

116
Visualizações
ngFor not update template and code in combineLatest run many times

I have a template

<div *ngFor="let item of list">
   {{item.name}}
</div>

Then in ts code.

ngOnInit() {
   const s0 = this.service.getList(this.id);
   const s2 = this.service.getOthers(this.id);
   combineLatest([s0, s1]).subscribe(([r0, r1]) => {
       this.list = r0;
       console.log('service is called');
   }
}

In another place, I have a button click event to add a new item to the list.

addItemToList(item: any) {
    this.service.addItem(item).subscribe(
        value => {
           console.log(value);
           //  then reload page by calling get list again
           this.service.getList(this.id).subscribe(
              res => this.list = res; // I want to refresh the view by this new list
              console.log(res);
             );
        }
     );
}

I am sure I added the new item successfully. But the view is not updating and the line console.log('service is called') in combineLatest was called many times. So the list is still the value when first time loading.(this.list = r0)

I can only update the view by click F5. I have tried ngZone or ChangeDetectorRef. Just not working....

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

0

It looks like you're trying to output two responses from your subscribe in

   combineLatest([s0, s1]).subscribe(([r0, r1]) => {
       this.list = r0;
       console.log('service is called');
   }

I think you need to have one response from your subscribe. Then split it after like:

   combineLatest([s0, s1]).subscribe(res => {
       this.list = res;
       ***split data***
       console.log('service is called');
   }

However if you want to differentiate the two observables you may have to nest their data in an object as when they are combined they will be returned as one response. Or you may not want to use combineLatest as it combines them.

about 4 years ago · Juan Pablo Isaza Relatório

0

I figured it out by myself. Just add take(1)

Before:

combineLatest([s0, s1]).subscribe(([r0, r1])

After:

combineLatest([s0, s1]).pipe(take(1)).subscribe(([r0, r1])
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