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

122
Visualizações
Smart search is not working when working with mergeMap

I want to use smart search in angular

<input type="text" [formControl]="leadForm" [matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete">
   <mat-option *ngFor="let item of results" [value]="item">
   {{item}}
   </mat-option>
</mat-autocomplete>

leadForm = new FormControl(null, Validators.required);

Then in ngOnInit()

 combineLatest([s1,s2,s3])
 .pipe(
 mergeMap(([r1, r2, r3]) => {
     this.x = r1;
     this.y = r2;
     this.names = r3;
     return of(1);
 }))
 .subscribe(() => {
     // display
 });
 this.leadForm.valuesChanges.subscribe(
     r => {
       if(r !== '') {
           this.results = this.names.filter(c => c.toLowerCase().includes(item.toLowerCase()));
       }
    }
 );

So the idea is to get the names from data stream then populate the dropdown. However sometimes it is not working. I guess that the stream order is wrong. We need to get this.names first. How to change the code?

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

0

Would be nice to know what s1 -- s3 is..

But in general you should refactor this so that results is an observable dependent on leadForm.valueChanges.

Something like:

this.results$ = this.leadForm.valueChanges.pipe(map(...));

Your problem here is that you subscribe 2 observables and expect that the order is defined, but there can be these cases for BOTH: it never resolves, one of them resolves first(last respectively), there is an error. To give you more advice I would need to know more about the case.. :/

about 4 years ago · Juan Pablo Isaza Relatório

0

Sounds like you need a more reactive approach where you declare variables for your Observables and react to changes in those variables.

Without the exact code, it's hard to get an exact example for you, but something like this:

names$ = this.http.get(...);

leadForm$ = this.leadForm.valueChanges();

results$ = combineLatest([names$, leadForm$]).pipe(
     map(([names, r]) => names.filter(c => c.toLowerCase().includes(r.toLowerCase()))
);

This should get you close.

You can then subscribe to results$ somewhere or bind directly to it using an async pipe.

I have an example of this technique here: https://www.youtube.com/watch?v=0EefbG6N3vY

And if you have a Pluralsight subscription, there is a complete walk through here: https://app.pluralsight.com/course-player?clipId=5732473c-ac12-450d-930c-096a687dd2d0

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