Ngx-bootstrap: How to use same typeahead on multiple fields

Created on 25 Feb 2017  路  4Comments  路  Source: valor-software/ngx-bootstrap

Hi, I am able to use the typeahead module on single field by creating an Observable and subscribing to it using a service. However not entirely sure how to use the same Observable for multiple form fields?

For instance i have 3 input fields on which i want to provide the same autocomplete options.

My TS file:

  private supplierName1: string = '';
  private supplierName2: string = '';
  private supplierName3: string = '';
  private suppliersListAutoComplete1: Observable<any>;
  private suppliersListAutoComplete2: Observable<any>;
  private suppliersListAutoComplete3: Observable<any>;


  constructor(
    private supplierService: SupplierService
  ) { 
    this.suppliersListAutoComplete1 = Observable.create((observer: any) => {
      this.supplierService.getSuppliersList(this.supplierName1).subscribe((result: any) => {
        observer.next(result);
      })
    });

    this.suppliersListAutoComplete2 = Observable.create((observer: any) => {
      this.supplierService.getSuppliersList(this.supplierName2).subscribe((result: any) => {
        observer.next(result);
      })
    });

    this.suppliersListAutoComplete3 = Observable.create((observer: any) => {
      this.supplierService.getSuppliersList(this.supplierName3).subscribe((result: any) => {
        observer.next(result);
      })
    });
  }

Anyway i can optimize this code?

comp(typeahead) question

Most helpful comment

Can anyone please suggest a solution for this problem :(

All 4 comments

No one who had similar scenario? :)

Can anyone please suggest a solution for this problem :(

I suppose at stack overflow its more chances to get an answer

I have the same issue, however I don't have an answer either. I need two different type ahead results

Was this page helpful?
0 / 5 - 0 ratings