Autocomplete with duplicate requests

Asked

Viewed 47 times

0

Hello, I wrote a autocomplete code, but the requests are duplicated every time I press a new key, from the amount greater than that stipulated in the filter. For example, when I type 4 keys, make 4 requests, when I type 5 keys, make 5 requests and so on. I already changed the HTML event to "change", but the problem persists.

     public autocomplete(){
        this.formTickets
            .get('nomeCliente')
            .valueChanges
            .pipe(filter(v => v.length > 3),
                  debounceTime(500),
                  distinctUntilChanged(),
                  switchMap(termo => this.clienteService.autocomplete(termo)),
            .subscribe(result => {
              
              let sucesso = result.success;
              if(sucesso == true){
                this.clientes = result.data;
              }
              
              console.log(this.clientes);
            });
      }
<div class="form-group col-md-10">
   <label class="mb-0">Nome Cliente</label>
   <input type="text" class="form-control" formControlName="nomeCliente" (change)="autocomplete()" list="dynmicUserIds"/>
   <datalist id="dynmicUserIds">
      <option *ngFor="let item of clientes">{{item.nome}}</option>
   </datalist>
</div>
  • 1

    you have to call no on init of your change inves ts

  • Eduardo, that’s right, I removed the change event and put the call on onInit and it worked perfectly. Thank you very much !

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.