Mat-autocomplete显示从服务器收到的前一个日期,而不是最晚的日期。如何修复它以显示收到的最新数据?

问题描述 投票:0回答:1
ngOnInit(): void {
    this.filteredAccountOptions = this.accountControl.valueChanges
    .pipe(
      startWith(''),
      map(value => this.accountFilter(value))
    );
    .
    .
    .

实际上,“ return this.createDialogService.accountOptions”语句在“ accountSearch(value)”方法的上一行中完成预订部分之前就已执行。这就是显示先前数据的原因。

private accountFilter(value: string): string[] {
    this.accountSearch(value);
    return this.createDialogService.accountOptions
  }
accountSearch(searchedValue: string) {
    this.createDialogService.searchAccount(searchedValue).subscribe(
      (success) => {
        this.createDialogService.accountOptions = JSON.parse(success.message)
      },
      (error) => {}
    )
  }

我的HTML

<input type="text" matInput [formControl]="accountControl" [matAutocomplete]="auto1">
    <mat-autocomplete #auto1="matAutocomplete">
        <mat-option *ngFor="let option of filteredAccountOptions | async" [value]="option.Name">
             {{option.Name}}
        </mat-option>
    </mat-autocomplete>
angular typescript observable subscription mat-autocomplete
1个回答
0
投票

由于您在返回Observable之后更改了数据,使用swithMap运算符。为避免服务器过载,请使用debounceTime

  ngOnInit(): void {
    this.filteredAccountOptions = this.accountControl.valueChanges.pipe(
      startWith(''),
      debounceTime(3000),
      switchMap(value => this._accountFilter(value)),
    );
  }

  private _accountSearch(searchedValue: string): Observable<any> {
    return this.createDialogService.searchAccount(searchedValue).pipe(
      map(success =>  JSON.parse(success.message)),
    );
  }
© www.soinside.com 2019 - 2024. All rights reserved.