AngularMaterial自动完成组件在字段更改发生之前不显示API数据
创始人
2024-10-28 12:00:46
0

如果您的Angular Material自动完成组件在字段更改发生之前不显示API数据,可能需要确保数据已正确获取并注入到组件中。另外,请检查是否使用了异步函数和rxjs操作符来处理数据,例如switchMap或mergeMap。

以下是一个示例代码,在使用自动完成组件时如何正确注入和处理API数据:

component.ts:

import { Component } from '@angular/core';
import { FormControl } from '@angular/forms';
import { Observable } from 'rxjs';
import { map, startWith } from 'rxjs/operators';
import { ApiService } from './api.service';

@Component({
  selector: 'app-autocomplete',
  templateUrl: './autocomplete.component.html',
  styleUrls: ['./autocomplete.component.scss']
})
export class AutocompleteComponent {
  myControl = new FormControl();
  options: string[] = [];
  filteredOptions: Observable;

  constructor(private apiService: ApiService) {}

  ngOnInit() {
    this.apiService.getData().subscribe((data: any) => {
      this.options = data;
      this.filteredOptions = this.myControl.valueChanges
        .pipe(
          startWith(''),
          map(value => this._filter(value))
        );
    });
  }

  private _filter(value: string): string[] {
    const filterValue = value.toLowerCase();
    return this.options.filter(option => option.toLowerCase().includes(filterValue));
  }
}

component.html:


  
  
    
      {{ option }}
    
  

在上面的代码示例中,我们注入了一个名为ApiService的服务,以获取API数据。在ngOnInit中,我们订阅了apiService.getData()方法,并将数据赋值到选项数组中。接着,我们使用了RxJS操作符来过滤和映射输入

相关内容

热门资讯

微扑克有辅助挂!红龙扑克是不是... 微扑克有辅助挂!红龙扑克是不是有问题(透视)都是是真的有挂(详细辅助玩家教你);微扑克有辅助挂!红龙...
aapoker挂!wopoke... aapoker挂!wopoker用ai有用吗(透视)从来是真的有挂(详细辅助可靠教程)1.aapok...
wepoke辅助插件!wopo... wepoke辅助插件!wopoker分析器下载(透视)切实存在有挂(详细辅助透明教程);人气非常高,...
aapoker透视辅助!德州a... aapoker透视辅助!德州ai辅助工具(透视)从来存在有挂(详细辅助教你攻略)1、构建自己的微扑克...
aapoker挂!aa扑克平台... aapoker挂!aa扑克平台的机制(透视)素来真的有挂(详细辅助爆料教程)1、不需要AI权限,帮助...
wepoke模拟器!wepok... wepoke模拟器!wepoke德州扑克(透视)素来有挂(详细辅助辅助教程),您好,wepoke模拟...
德扑之星作弊!wepoker真... 德扑之星作弊!wepoker真的有挂吗(透视)先前存在有挂(详细辅助技巧教程);致您一封信;亲爱德扑...
aapoker透视辅助!wep... aapoker透视辅助!wepower辅助软件(透视)先前是有挂(详细辅助透明挂教程)1、aapok...
aapoker俱乐部!德扑线上... aapoker俱乐部!德扑线上有机器人吗(透视)确实真的有挂(详细辅助德州教程)是一款可以让一直输的...
wepokeai机器人!wep... wepokeai机器人!wepoke挂透视(透视)其实存在有挂(详细辅助透牌教程)1、不需要AI权限...