【发布时间】:2021-11-04 15:38:34
【问题描述】:
我有一个预先输入的下拉菜单,当您单击它时,它会显示过时的值,所以我想使它在单击它时不显示任何内容(直到您输入)。预输入功能本身正在运行。
最初我认为必须在我的组件文件中进行更改(例如清空字段),但看起来它可能在模板中进行(或在 comp 和模板文件中)。
我对 Angular 很陌生,所以如果我的语法/代码/等生锈了,我深表歉意。
组件文件:
// locations
filteredLocations: Observable<any[]>;
filteredUserLocations: Observable<any[]>;
locationTargeting = 'My Location';
getMyLocations(search: string): void {
const user = this.auth.account$.getValue();
if (!search.includes(';') && this.locationTargeting === 'My Location') {
// this.filteredUserLocations = new Observable<any[]>(); // tried setting this here (and in other locations), but the typeahead history still appears
this.filteredUserLocations = this.locationService
.getTypeaheadLocationsByValue(
search, // value
user.id, // accountId
null, // accountIds
this.targetingFormGroup.get('groups')?.value) // groupIds
.pipe(map((result: CollectionResponse<LocationSearchResponse>) => {
if (result.list) {
result.list.map((location) => this.addToLocationCache(location.id, location.locationNumber, location.name));
}
return result.list && result.list.length > 0 ? [{ label: 'User Locations', values: (result.list || []) }] : [];
}));
} else {
this.filteredUserLocations = new Observable<any[]>();
}
}
服务文件:
getTypeaheadLocationsByValue(
value: string,
accountId: string,
accountIds: string[] = null,
groupIds: string[] = null
): Observable<CollectionResponse<LocationSearchResponse>> {
const searchLocationTypeahead: SearchLocationsTypeaheadByValueRequest = new SearchLocationsTypeaheadByValueRequest();
if (accountIds) {
// etc
}
if (groupIds) {
// etc
}
searchLocationTypeahead.value = value;
searchLocationTypeahead.accountId = accountId;
return this.http.post<CollectionResponse<LocationSearchResponse>>('my-url', searchLocationTypeahead);
}
模板文件:
<div *ngIf="hasLocationTargeting" class="add-container">
<mat-form-field>
<input autofocus matInput formControlName="locationToAdd"
data-atm="target-locations-input" [matAutocomplete]="location">
</mat-form-field>
</div>
【问题讨论】:
标签: angular input rxjs observable typeahead