Angular Material 自动完成显示 TypeError:无法读取未定义的属性“createEmbeddedView”

use*_*292 7 typescript angular-material angular

我正在尝试使用角度材料自动完成功能。但是当我保持专注时,我开始收到错误:无法读取未定义的属性“createEmbeddedView”

对于我输入的每个字母,我也会收到另一个错误ERROR TypeError: this.autocomplete._setVisibility is not a function有人可以解释一下我的代码有什么问题吗?我是角度的初学者。

在我的 html 上,我有:

<mat-form-field> 
<input formControlName="accId" matAutocomplete="auto" matInput> 
<mat-autocomplete #auto="matAutocomplete">                                                                                                                                                                                                                                                                                                                                                                                         
<mat-option *ngFor="let accId of filteredOptions">  {{accId}}  
</mat-option>                                                                                                                                                                                                                                    
</mat-autocomplete>
</mat-form-field> 
Run Code Online (Sandbox Code Playgroud)

我的 .ts 文件:

 filteredOptions: Observable<string[]>;
 ngOnInit(): void {    
    this.filteredOptions = this.form.controls['accId'].valueChanges.pipe(startWith(''),map(val => this.filter(val)));
    console.log(this.filteredOptions);
    }

  filter(val: string): string[] {
  console.log("Inside filter...");
  return this.details.listOfAccIds.filter(option =>option.toLowerCase().includes(val.toLowerCase()));
  }
Run Code Online (Sandbox Code Playgroud)

注意:我得到 this.details 作为

Detail {listOfAccIds: Array(3), listOfCodes: Array(2), listOfReasons: Array(3)}
  listOfAccIds:(3) ["altaccId", "altaccIdss2", "altiid33"]
  listOfCodes:(2) ["code1", "code2"]
  listOfReasons:(3) ["reason1", "reason2", "reason3"]
Run Code Online (Sandbox Code Playgroud)

Baz*_*Baz 7

您缺少 matAutocomplete 指令周围的括号:

<input formControlName="accId" matAutocomplete="auto" matInput>
Run Code Online (Sandbox Code Playgroud)

应该

<input formControlName="accId" [matAutocomplete]="auto" matInput>
Run Code Online (Sandbox Code Playgroud)

括号是必需的,因为您想将引用传递给变量auto。您编写它的方式仅将静态字符串传递给指令,这不起作用,因为指令需要元素引用而不仅仅是其名称。