angular - Angular中的自动完成文本框不适用于角度材料?
问题描述
我正在尝试在来自带有 Angular 材料的 API 的 Angular 数据中创建一个自动完成文本框,但它不起作用,控制台中也没有显示任何错误,只显示空数组。 尽管自动完成请求在网络中显示为 Preflight,但我正在使用 API 中的 Title 字段自动完成。
API JSON 数据:
Id: 1
Title: "Zheng"
WebSite: "www.tokyo.com"
city: "Tokyo"
组件.html
<input matInput placeholder="company1" class="input-text js-input" [matAutocomplete]="auto" [formControl]="searchPublicationCtrl">
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let publication of filteredPublication" [value]="publication.Title">
<mat-option *ngIf="isLoading" class="is-loading">Loading...</mat-option>
<span>{{publication.Title}}</span>
</mat-option>
</mat-autocomplete>
组件.ts
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Router } from '@angular/router';
import { AppServiceService } from './../app-service.service';
import { Subscriber } from 'rxjs';
import { FormControl } from '@angular/forms';
import { debounceTime, tap, switchMap, finalize } from 'rxjs/operators';
@Component({
selector: 'app-main',
templateUrl: './main.component.html',
styleUrls: ['./main.component.css']
})
export class MainComponent implements OnInit {
searchPublicationCtrl = new FormControl();
filteredPublication: any;
isLoading = false;
errorMsg: string;
constructor(private http: HttpClient,private auth : AppServiceService, private _router: Router) {
this.auth.getPublication().subscribe(
data => {
console.warn(data);
},
error =>{
console.log("Publication Error",error);
}
)
}
ngOnInit(): void {
this.searchPublicationCtrl.valueChanges
.pipe(
debounceTime(500),
tap(() => {
this.errorMsg = "";
this.filteredPublication = [];
this.isLoading = true;
}),
switchMap(value => this.http.post("http://localhost:3000/articles/publicationData",{} + value)
.pipe(
finalize(() => {
this.isLoading = false
}),
)
)
)
.subscribe(data => {
if (data['Search'] == undefined) {
this.errorMsg = data['Error'];
this.filteredPublication = [];
} else {
this.errorMsg = "";
this.filteredPublication = data['Search'];
}
console.log(this.filteredPublication);
});
}
}
解决方案
推荐阅读
- mysql - SQL LEFT JOIN 从第二个表中排除两条记录
- r - d 素数表示误报率大于 R 中的命中率
- mongodb - Golang MongoDB 将十进制值更改为 int
- azure - Ssis 任务在 Azure 集成运行时中运行缓慢。相同的包在较低配置的较低环境中运行速度很快
- macos - MacOS 找不到 Qt5 路径
- python - mysql.connector.errors.ProgrammingError:光标未连接
- python - 将列表列表转换为 json 格式(第一个列表是标题)
- postfix-mta - 后缀虚拟域地址不起作用
- visual-studio-code - 录制“宏”?还是 Visual Studio Code 中的一系列操作?
- javascript - 将对象数组转换为数组 node.js