简体   繁体   English

mat-autocomplete 选项无法找到属性

[英]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.

相关问题 如何从另一个组件更新 mat-autocomplete 选项? - How to update mat-autocomplete options from another component? mat-autocomplete:选择后需要重置选项列表 - mat-autocomplete: Need to Reset the List of Options After a Selection is Made 如何在其他mat-autocomplete更改时绑定mat-autocomplete? - How to bind mat-autocomplete on change of other mat-autocomplete? 垫子自动完成不允许我 select - mat-autocomplete not allowing me to select 垫子自动完成中的无限滚动 angular 11 - Infinite scroll in mat-autocomplete angular 11 Mat-autocomplete 在 ngOnInit() 中不起作用,Map() 不起作用 - Mat-autocomplete is not working in ngOnInit(), Map() is not working 如何在 formBuilder 上禁用 mat-autocomplete? - How to disable mat-autocomplete on formBuilder? 如何通过@input 属性应用样式,我想在 angular 中增加 mat-autocomplete 的宽度 - How to apply style through @input property, I want to increase width of mat-autocomplete in angular 如何显示名称但从 mat-autocomplete select 的对象中获取某些其他属性的值? 角-角材料 - How to display a name but take the values of some other property from the object from mat-autocomplete select? Angular- Angular Material Angular5 - 如何检测垫子自动完成中的空字段或已删除字段? - Angular5 - How to detect empty or deleted field in mat-autocomplete?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM