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怎么拿好牌... 近日!aapoker怎么拿好牌(透视)果然是真的辅助软件(有挂方法)-哔哩哔哩1.aapoker怎么...
透视计算!pokemmo脚本辅... 透视计算!pokemmo脚本辅助器下载(透视)果然存在有辅助神器(确实有挂)-哔哩哔哩1、很好的工具...
据监测!德州局脚本(透视)竟然... 据监测!德州局脚本(透视)竟然真的有辅助工具(新版有挂)-哔哩哔哩1、任何德州局脚本透视是真的假的的...
最终!wepoker怎么破解游... 最终!wepoker怎么破解游戏(透视)确实真的有辅助平台(有挂技术)-哔哩哔哩wepoker怎么破...
透视规律!pokemmo手机版... 透视规律!pokemmo手机版透视脚本(透视)都是是真的辅助神器(的确有挂)-哔哩哔哩1.pokem...
据文件显示!wepoker模拟... 据文件显示!wepoker模拟器哪个好用(透视)原来是真的辅助工具(有挂教学)-哔哩哔哩1、实时we...
透视ai代打!pokeplus... 透视ai代打!pokeplus脚本(透视)本来真的有辅助平台(有挂教学)-哔哩哔哩1、pokeplu...
受玩家影响!竞技联盟辅助(透视... 受玩家影响!竞技联盟辅助(透视)原来是真的辅助插件(有挂方式)-哔哩哔哩亲,关键说明,竞技联盟辅助透...
透视新版!德州局怎么透视(透视... 透视新版!德州局怎么透视(透视)本来真的是有辅助app(揭秘有挂)-哔哩哔哩;1、该软件可以轻松地帮...
此事备受玩家关注!wepoke... 此事备受玩家关注!wepoker底牌透视(透视)切实是真的辅助工具(有挂方法)-哔哩哔哩wepoke...