这是我正在使用的表格:
<form [formGroup]="searchForm" id="searchForm">
<mat-form-field>
<input
matInput
type="text"
name="awesome"
id="awesome"
[formControl] = "formCtrl"
[matAutocomplete] = "auto"
value="{{ awesomeText }}"
[matAutocomplete]="auto">
<mat-autocomplete #auto = "matAutocomplete">
<mat-option *ngFor = "let res of result | async" [value] = "res">
{{res}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
</form>
这是里面constructor()
:
this.formCtrl = new FormControl();
this.formCtrl.valueChanges.subscribe((newValue) => {
this.result = this.find(newValue);
console.log('yes');
});
正在打印,yes
所以我知道这是有效的,但mat-autocomplete
没有显示任何内容。该result
变量也在更新,因为我可以看到它在控制台上打印。我无法理解为什么没有显示搜索到的值。
我将不胜感激任何帮助!
编辑
这是find()
方法:
find(val: string): string[] {
const matchFound = [];
for (let i = 0; i < dataJson.length; i++) {
if (dataJson[i].text.toLowerCase().startsWith(val) || dataJson[i].text.startsWith(val)) {
matchFound.push(dataJson[i].text);
}
}
console.log('matches ' + matchFound);
return matchFound;
}