【发布时间】:2020-07-01 16:05:07
【问题描述】:
在最新版本的 Angular 中,我使用了以下 HTML:
<mat-autocomplete #auto="matAutocomplete" [displayWith]="displayFn">
<mat-option *ngFor="let entity of entityNames" [value]="entity.EntityId">
{{ entity.EntityName }}
</mat-option>
在我的常规角度组件中,displayFn 方法如下所示:
displayFn(entityId: number): string {
const name = (entityId && entityId > 0) ? this.entityNames?.find(entityName => entityName.EntityId === entityId).EntityName : '';
return name;
}
问题是,我的组件“this”。不可用,对其成员的任何访问都失败。
【问题讨论】:
-
您可以将回调定义为箭头函数:
displayFn = (entityId: number): string => { ... }。
标签: angular callback autocomplete angular-material this