您需要使用
[displayWith]
属性自定义所选选项的显示文本。
displayFn(tokenName: any): string {
return tokenName && tokenName.displayName ? tokenName.displayName : '';
}
<mat-autocomplete #auto="matAutocomplete" [displayWith]="displayFn">
...
</mat-autocomplete>
参考
Setting separate control and display values
对于您报告的问题:
-
更改
FormControl<string>
到
FormControl<string | TokenNameWithId>
类型
-
修改
_filter
检查的函数
value
类型
public tokenNamesControl = new FormControl<string | TokenNameWithId>('', {
nonNullable: true,
});
private _filter(value: string | TokenNameWithId, tokenNamesWithId: TokenNameWithId[]): any[] {
const filterValue =
typeof value === 'string'
? value.toLowerCase()
: value.displayName.toLowerCase();
return tokenNamesWithId.filter((tokenNameWithId: any) =>
tokenNameWithId.displayName.toLowerCase().includes(filterValue)
);
}