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

相关内容

热门资讯

透视有挂"哈糖大菠萝... 透视有挂"哈糖大菠萝有挂吗"详细辅助解密教程(私人局俱乐部神器)1、点击下载安装,微扑克wpk插件透...
透视辅助"wepok... 自定义wepoker好友房开挂系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅助...
透视工具"wejok... 透视工具"wejoker辅助器怎么卖"详细辅助玩家教程(可以买辅助教程);致您一封信;亲爱wejok...
透视app"拱趴大菠... 1、透视app"拱趴大菠萝挂哪里"详细辅助攻略教程(科技辅助器神器);详细教程。2、拱趴大菠萝挂哪里...
透视免费"wepok... 1、透视免费"wepoker怎么看牌型"详细辅助分享教程(正确养号方法透明)。2、wepoker怎么...
透视透视"wepok... 您好,wepoker数据分析这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多...
透视挂"aapoke... 透视挂"aapoker脚本怎么用"详细辅助切实教程(透视有用技巧);建议优先通过aapoker脚本怎...
透视辅助"wepok... 透视辅助"wepoker辅助器"详细辅助解密教程(辅助器下载教程)是一款可以让一直输的玩家,快速成为...
透视讲解"wepok... 透视讲解"wepoker破解游戏盒子"详细辅助科技教程(透视脚本技巧)这是由厦门游乐互动科技有限公司...
透视ai代打"hhp... 透视ai代打"hhpoker俱乐部是干嘛的"详细辅助安装教程(可以看底牌透视)是一款可以让一直输的玩...