0

I have used mat-autocomplete with Angular 7.

<mat-form-field floatLabel="never">
  <input matInput aria-label="salesRepresentative" type="text" [placeholder]="translationObj.startTypingPlaceholder" autocomplete="off"
    [formControl]="autoCompleteControl" [matAutocomplete]="auto">
  <mat-icon matSuffix class="cursor-pointer">search</mat-icon>
  <mat-autocomplete #auto="matAutocomplete" [displayWith]="createSalesRepString">
    <mat-option *ngFor="let item of salesPickerAutoComplete$ | async;" [value]="item">
      {{item.FirstName}} {{item.LastName}} - (Username: {{item.Username}})
    </mat-option>
  </mat-autocomplete>
</mat-form-field>

And below is my component code.

this.salesPickerAutoComplete$ = this.autoCompleteControl.valueChanges.pipe(
  startWith(''),
  debounceTime(400),
  distinctUntilChanged(),
  switchMap(value => {
    if (value && value.length > 2) {
      return this.getSalesReps(value);
    } else {
      return of(null);
    }
  })
);

getSalesReps(value: string): Observable<any[]> {
  const params = new QueryDto;
  params.$filter = `substringof('${value}',FirstName) or substringof('${value}',LastName)`;
  params.$select = "UserId,FirstName,LastName,Username";
  return from(this.salesRepresentativesService.GetSalesRepresentatives(params))
    .pipe(
      map(response => response.Data),
      catchError(() => { return of(null) })
    );
}

Now this works perfect with search by typing in input.

My issue is i want list to auto-populate on-load with some items.

I am not sure how to do this with this structure.

Can anyone tell me how can i do that? How can i push/change some items in mat-autocomplete dynamically?

Ankur Akvaliya
  • 2,989
  • 4
  • 28
  • 53
  • take a look at this conversation : https://stackoverflow.com/questions/52914319/angular-mat-autocomplete-dynamic-add-delete-item-issue – Amit Baranes Mar 30 '19 at 21:01
  • Is this duplicate of https://stackoverflow.com/questions/55453366/angular-dynamically-push-data-to-observable-without-changing-value-in-input – Jeba Prince Apr 01 '19 at 11:25

0 Answers0