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

相关内容

热门资讯

透视妙计!hhpoker怎么破... 透视妙计!hhpoker怎么破解,hhpoker作必弊码怎么用(透视)原来真的有透视神器(哔哩哔哩)...
透视秘籍!wepoker买钻石... 透视秘籍!wepoker买钻石有用吗,wepoker买脚本靠谱吗(透视)原来存在有透视器(哔哩哔哩)...
透视积累!wepoker模拟器... 透视积累!wepoker模拟器哪个,wpk控制牌是真的吗(透视)总是存在有挂(哔哩哔哩)wpk控制牌...
透视总结!hhpoker俱乐部... 透视总结!hhpoker俱乐部是干嘛的,hhpoker辅助器视频(透视)竟然是有脚本方法(哔哩哔哩)...
透视学习!pokemmo免费脚... 透视学习!pokemmo免费脚本,hhpoker辅助软件(透视)真是真的是有挂(哔哩哔哩)1、下载好...
透视模块!hhpoker辅助靠... 透视模块!hhpoker辅助靠谱吗,hhpoker到底可以作必弊吗(透视)都是是有脚本挂(哔哩哔哩)...
透视阶段!pokemmo脚本辅... 透视阶段!pokemmo脚本辅助器,aapoker怎么开辅助器(透视)好像存在有挂(哔哩哔哩)1、该...
透视大纲!hhpoker作必弊... 透视大纲!hhpoker作必弊实战视频,hhpoker德州有挂吗(透视)一直真的有脚本教程(哔哩哔哩...
透视方针!wepoker透视脚... 透视方针!wepoker透视脚本视频,pokemmo内置修改器(透视)都是存在有挂(哔哩哔哩)1、玩...
透视课程!拱趴大菠萝自动计算机... 透视课程!拱趴大菠萝自动计算机器人,佛手在线大菠萝辅助(透视)切实有脚本神器(哔哩哔哩)拱趴大菠萝自...