Angular material AutoComplete 现场服务调用拉取数据的速度比用户输入的慢
Angular materialAutoComplete field service call pulls data slower than user types in
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中实现一个完美的数据。如何match/overcome用户在字段中绑定的速度和从服务中获取的输出显示在自动完成面板中?
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,请检查并告诉我为什么在语句映射中 result.contacts 出现错误(结果 => result.contacts)
尝试使用 switchMap
。它将确保结果始终是新鲜的。
On each emission the previous inner observable (the result of the function you supplied) is cancelled and the new observable is subscribed
filteredCustomersByName
this.filteredCustomersByName = this.customerNameControl.valueChanges.pipe(
startWith(''),
debounceTime(200),
switchMap(val => val.length > 2
? this.appData.get(this.appData.url.getCustomerByName, [val])
: of([])
),
map(result => result.customers)
);
filterCustomerByCode
this.filterCustomerByCode = this.customerNameControl.valueChanges.pipe(
startWith(''),
debounceTime(200),
switchMap((val: string) => {
if (val.length > 2 || val.charAt(0).toLowerCase() === 'b') {
const mappedVal = ('000000000000' + val).substr(-10);
return this.appData.get(this.appData.url.getCustomerByName, [mappedVal]);
}
return of([]);
}),
map(result => result.customers)
);
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中实现一个完美的数据。如何match/overcome用户在字段中绑定的速度和从服务中获取的输出显示在自动完成面板中?
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,请检查并告诉我为什么在语句映射中 result.contacts 出现错误(结果 => result.contacts)
尝试使用 switchMap
。它将确保结果始终是新鲜的。
On each emission the previous inner observable (the result of the function you supplied) is cancelled and the new observable is subscribed
filteredCustomersByName
this.filteredCustomersByName = this.customerNameControl.valueChanges.pipe(
startWith(''),
debounceTime(200),
switchMap(val => val.length > 2
? this.appData.get(this.appData.url.getCustomerByName, [val])
: of([])
),
map(result => result.customers)
);
filterCustomerByCode
this.filterCustomerByCode = this.customerNameControl.valueChanges.pipe(
startWith(''),
debounceTime(200),
switchMap((val: string) => {
if (val.length > 2 || val.charAt(0).toLowerCase() === 'b') {
const mappedVal = ('000000000000' + val).substr(-10);
return this.appData.get(this.appData.url.getCustomerByName, [mappedVal]);
}
return of([]);
}),
map(result => result.customers)
);