Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

128
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda