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操作符来过滤和映射输入

相关内容

热门资讯

科技透视"wepok... 科技透视"wepoker辅助器教程"开挂(透视)辅助平台(推荐几款新版AA德州教程)科技透视"wep...
传授开挂!四川辅助工具,小程序... 传授开挂!四川辅助工具,小程序牵手跑得有开挂吗(太夸张了辅助开挂脚本);1.小程序牵手跑得有开挂吗 ...
通报透视"wepok... 通报透视"wepoker真的有挂透视辅助吗"开挂(透视)辅助神器(最新技巧揭秘攻略)【福星临门,好运...
教会辅助!蜀山四川破解版安卓版... 教会辅助!蜀山四川破解版安卓版,逸趣鄱阳翻精辅助(太实锤了辅助开挂下载),蜀山四川破解版安卓版是用手...
正版透视"wepok... 正版透视"wepoker德州透视软件是真的吗"开挂(透视)辅助平台(科技介绍攻略方法)正版透视"we...
科技辅助!腾旭欢乐全集辅助,创... 科技辅助!腾旭欢乐全集辅助,创思维激k看底牌辅助(太离谱了开挂辅助工具);大家肯定在之前创思维激k看...
通报透视"wepok... 通报透视"wepoker透视教程"开挂(透视)辅助平台(玩家必看科普详细教程)通报透视"wepoke...
传授辅助!微乐斗地主有挂吗,小... 传授辅助!微乐斗地主有挂吗,小程序牵手跑得辅助(太夸张了开挂辅助安装);1、不需要AI权限,帮助你快...
细节透视"wepok... 细节透视"wepoker是不是有辅助"开挂(透视)辅助软件(玩家必看秘籍解密教程)细节透视"wepo...
细节开挂!衡阳丫丫辅助,玩吧辅... 细节开挂!衡阳丫丫辅助,玩吧辅助工具(太坑了开挂辅助平台),玩吧辅助工具是用手机号来登录游戏的系统规...