Angular Material自动完成显示TypeError:无法读取undefined的属性'createEmbeddedView'

时间:2018-06-04 22:34:54

标签: angular typescript angular-material

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

我输入的每个字母都会出现另一个错误 ERROR TypeError:this.autocomplete._setVisibility不是函数有人可以解释我的代码有什么问题吗?我是角色的初学者。

在我的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> 

我的.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()));
  }

注意:我收到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"]

1 个答案:

答案 0 :(得分:4)

您缺少关于matAutocomplete指令的方括号:

<input formControlName="accId" matAutocomplete="auto" matInput>

应该是

<input formControlName="accId" [matAutocomplete]="auto" matInput>

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