【发布时间】:2020-08-04 22:54:22
【问题描述】:
这是我正在使用的表格:
<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;
}
【问题讨论】:
-
你的方法
this.find是做什么的?发布您的代码 -
this.result 应该包含匹配字符串的数组。 this.result 在您共享的代码中包含什么内容?
-
@Shank 更新了代码
标签: angular autocomplete angular-material angular10