this.filteredContact = this.contactControl.valueChanges.pipe(
startWith(''),
debounceTime(200),
map(val => this.filterContact(val))
);
filterContact(val: string): any {
if (val.length > 2) {
return _.filter(
this.contacts,
item => item.name.toLowerCase().indexOf(val.toLowerCase()) !== -1
);
}
}
this.filteredContact = this.contactControl.valueChanges.pipe(
startWith(''),
debounceTime(200),
switchMap(
val =>
val.length > 2 ?
_.filter(
this.contacts,
item => item.name.toLowerCase().indexOf(val.toLowerCase()) !== -1
) :
Observable.of([])
),
map(result => result.contacts)
);
我正在使用 Angular materialAutoComplete 字段,其中服务调用会在用户键入数据时提取数据。由于用户键入的速度比服务提取数据的速度快,因此 autocompleteSelection 面板中实际显示的内容没有同步。如何在 materialAutoComplete 中实现完美的数据。如何匹配/克服用户在字段中绑定的速度以及从服务中获取的输出以显示在 autocompletePanel 中?
public filteredCustomersByName: Observable<e.ICustomer[]>;
this.filteredCustomersByName = this.customerNameControl.valueChanges.pipe(
startWith(''),
debounceTime(200),
map(val => this.filterCustomersByName(val))
);
filterCustomersByName(val: string): any {
if (val.length > 2) {
this.appData.get(this.appData.url.getCustomerByName, [val]).subscribe(
result => {
this.customersByName = result.customers;
},
err => {
console.log('Error ' + err.message);
this.toastr.error(err.message);
}
);
return this.customersByName;
}
}
<div class="box-row">
<mat-form-field>
<input placeholder="Customer Name" type="text" #customerNameId matInput [formControl]="customerNameControl" [(ngModel)]='selectedCustomerName'
[matAutocomplete]="customerName" required>
<mat-autocomplete #customerName="matAutocomplete" class='customerDetails'>
<mat-option (click)="setCustomerDetails(customer)" *ngFor="let customer of filteredCustomersByName | async" [value]="customer.name">
{{ customer.name}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
</div>
提前致谢。
filterCustomerByCode(val: string): any {
if (val.length > 2) {
if (val.charAt(0).toLowerCase() !== 'b') {
val = ('000000000000' + val).substr(-10);
}
this.appData.get(this.appData.url.getCustomerByCode, [val]).subscribe(
result => {
this.customersByCode = result.customers;
},
err => {
console.log('Error ' + err.message);
this.toastr.error(err.message);
}
);
return this.customersByCode;
}
}
我尝试使用switchMap更改filteredContact,请检查并告诉我为什么在语句map(result => result.contacts)中的result.contacts处出现错误