我遇到了 Angular 自动完成的问题,我无法显示名称而不是 id,正如您在此处看到的那样:
我的数据是 Observable 的,如下所示:
autocomplete.component.ts:
driverListItem$: Observable<DriverListItem[]>;
ngOnInit() {
this.driverListItem$ = this.driverListItemService.getList(+subcontractorId);
}
自动完成.service.ts:
getList(subcontractorId?: number): Observable<DriverListItem[]> {
return this.http.get<DriverListItem[]>(
this.apiUrl,
{
params: new HttpParams().append('subcontractorId', subcontractorId.toString()),
headers: this.getAuthHeaders(this.authService.token),
}
)
.do(
value => console.debug('DriverListItemService getList value', value),
error => {
console.debug('DriverListItemService getList error', error);
this.handleError(error);
},
);
}
autocomplete.component.html:
<mat-form-field style="width:100px">
<input matInput name="assignedOperator" [(ngModel)]="entity.assignedOperator" [matAutocomplete]="auto" />
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let driver of driverListItem$ | async" [value]="driver.id">{{ driver.firstName + ' ' + driver.lastName }}</mat-option>
</mat-autocomplete>
</mat-form-field>