[英]mat-autocomplete options not able to find property
I am try to do autocomplete using this code.我正在尝试使用此代码进行自动完成。
Now, the issue is I receiving option as [object objecct]
, I understood that's because I need to specify the field like option.name
but when I do getting does not exist error
.现在,问题是我收到的选项为
[object objecct]
,我知道那是因为我需要指定像option.name
这样的字段,但是当我收到does not exist error
时。 To check I options empty or Not I print it.要检查我的选项是否为空或不是我打印它。 like below
像下面
private _filter(value: string): string[] {
if (value && value !== '') {
const filterValue = value.toLowerCase();
console.log('this',options)
return this.options.filter(
(option) =>
option.name.toLowerCase().includes(filterValue) ||
option.clock.toString().startsWith(filterValue)
);
} else {
return [];
}
output: output:
enter image description here在此处输入图像描述
Note: My code is almost same as above reference, just I am map specific fields from another array.注意:我的代码与上面的参考几乎相同,只是我是另一个数组中的 map 特定字段。
retrieveAbsence():void{
this.employeeService.getAll()
.subscribe(
data => {
this.employee = data;
console.log(data);
},
error => {
console.log(error);
});
}
this.options = this.employee.map(((i) => {
return {
id: i.id,
clock: i.clock,
payroll:i.payroll,
name: i.firstName+' '+i.surName
}}));
Output: James Smith Output:詹姆斯·史密斯
You are using value of a dropdown item as and object, but you need a string.您将下拉项的值用作 object,但您需要一个字符串。 Just change
[value]="option"
to [value]="option.name"
只需将
[value]="option"
更改为[value]="option.name"
<mat-option
*ngFor="let option of filteredOptions | async"
[value]="option"
>
{{option.name}}
</mat-option>
to到
<mat-option
*ngFor="let option of filteredOptions | async"
[value]="option.name"
>
{{option.name}}
</mat-option>
Try with {{option['name]}}
also to get the value you will need to [value]="option['name']"
尝试使用
{{option['name]}}
也可以得到你需要的值[value]="option['name']"
In the mat-option, the value should be option.name rather than complete object. As you are filtering from an array of object. you should assign the single value not the complete object to value.在 mat-option 中,值应该是option.name而不是完整的 object。当您从 object 的数组中过滤时。您应该将单个值而不是完整的 object 分配给值。
<mat-option *ngFor="let option of filteredOptions | async[value]="option.name">
{{option.name}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.