【发布时间】:2026-01-18 20:30:01
【问题描述】:
我有一个材料自动完成功能。
我正在调用 ngrx 来提取数据。
//parentcomponent.ts
this.store.select(fromStore.getSearchFormStateMessageTypeListData).subscribe(msgTypeList => {
if (msgTypeList.length > 0) {
console.log('msgtypelist ='+msgTypeList)
for (var i = 0; i < msgTypeList.length; i++) {
this.messageTypeList.push(msgTypeList[i]);
}
}
else {
this.store.dispatch(new fromStore.GetGlobalSearchMessageTypeList({}));
}
})
//parentcomponent.html
<mat-card style="margin: 1px;">
<search-form [messageTypeList]="messageTypeList" (onSearchData)="searchButtonClick($event)" [rowData]="rowData | async">
</search-form>
</mat-card>
从父级我将 msgTypeList 传递给子级。
在孩子中,我将自动完成绑定到列表,但是当我们单击内部时,列表没有显示任何内容。
它只在我们在输入框中输入内容时显示选项(过滤选项)
//childcomponent.html
<form [formGroup]="searchForm" id="searchForm" style="width:100%;height:70%" (ngSubmit)="onSubmit()">
<tr>
<td class="input-form" style="padding-right:4%;width:10%">
<mat-form-field>
<input type="text" placeholder="Message Type" aria-label="Assignee" formControlName="msgType" matInput [matAutocomplete]="autoMsgType">
<mat-autocomplete #autoMsgType="matAutocomplete" placeholder="Message Type" [displayWith]="displayMessageTypeFn">
<mat-option *ngFor="let messageType of filteredMessageTypeList | async | sort:'msgType'" [value]="messageType">{{messageType.msgType}}</mat-option>
</mat-autocomplete>
</mat-form-field>
</td>
</tr>
</form>
下面是child.ts文件
//childcomponent.ts
searchForm: FormGroup;
ngOnInit() {
this.searchForm = this.formBuilder.group({
direction: [null],
msgType: [null, Validators.nullValidator],
});
this.filterMessageTypeLists();
}
filterMessageTypeLists() {
this.filteredMessageTypeList = this.searchForm.controls['msgType'].valueChanges.pipe(
startWith<string | any>(''),
map(value => typeof value === 'string' ? value : value.msgType),
map(msgType => msgType ? this._msg_filter(msgType, this.messageTypeList) : this.messageTypeList.slice())
);
}
private _msg_filter(msgType: string, lists: any[]): any[] {
const filterValue = msgType.toLowerCase();
return lists.filter(option => option.msgType.toLowerCase().includes(msgType.toLowerCase()))
}
displayMessageTypeFn(field?: any): string | undefined {
return field ? field.msgType : undefined;;
}
问题是,如果我点击自动完成输入,它应该会打开列表但没有显示
【问题讨论】:
-
我必须先将空字符串设置为 formControl,以便在输入任何内容之前显示自动完成下拉列表:
this.searchForm.controls['msgType'].setValue('');
标签: angular list autocomplete angular-material angular6