Angular 自动完成数据绑定问题-角度材质
我使用角度/材质自动完成。我在输入框中输入几个字符(abc),并将内容快速更新为(xyz)。在自动完成建议中,我仍然得到abc的值。 尽管后端API返回适当的值。我无法使用更新的值更新DOM。 我尝试使用aync管道和可观测设备。请给我一个更好的解决方法 注意:此问题仅在快速更新输入文本时发生Angular 自动完成数据绑定问题-角度材质,angular,typescript,async-await,autocomplete,angular-material,Angular,Typescript,Async Await,Autocomplete,Angular Material,我使用角度/材质自动完成。我在输入框中输入几个字符(abc),并将内容快速更新为(xyz)。在自动完成建议中,我仍然得到abc的值。 尽管后端API返回适当的值。我无法使用更新的值更新DOM。 我尝试使用aync管道和可观测设备。请给我一个更好的解决方法 注意:此问题仅在快速更新输入文本时发生 <mat-form-field class="text-full-width associate" *ngIf="isEditMode && selec
<mat-form-field class="text-full-width associate" *ngIf="isEditMode && selectionEditIndexData.includes(row.rowIndex)">
<input matInput formControlName="associateName"
[matAutocomplete]="auto" (keydown.enter)="$event.preventDefault()" (keyup)="getAssocNameByName($event,$event.target.value,row.rowIndex)">
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let option of filteredOptions | async" [value]="option.fullName" (onSelectionChange)="getAssociateNameById(row.rowIndex,option.associateId)">
{{option.fullName}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
`
{{option.fullName}
`
角度代码:
`
getAssocNameByName(事件:KeyboardEvent,值:string,行索引:string){
this.SearchControl=this.tableForm.controls.reourcesFormArray['controls'][rowIndex].controls.associateName
如果(event.keyCode!==40&&event.keyCode!==38&&value.length>0){
this.resourceAssignmentService.getAssociateNamesByName(值,this.id).pipe(takeUntil(this.onDestroy$)).subscribe((successRes:NamesList[])=>{
this.options=新数组();
successRes.forEach(元素=>{
this.options.push(元素)
},
this.filteredOptions=this.SearchControl.valueChanges
.烟斗(
startWith(“”),
映射(value=>this.\u过滤器(value,this.options))
));
},
);
}
私有_筛选器(值:字符串,选项:名称列表[]):名称列表[]{
常量filterValue=value.toLowerCase();
返回options.filter(option=>(option.fullName.toLowerCase().indexOf(filterValue)==0)| | option.fullName.toUpperCase().indexOf(filterValue)==0);
}
`
getAssocNameByName(event: KeyboardEvent, value: string, rowIndex: string) {
this.SearchControl = this.tableForm.controls.reourcesFormArray['controls'][rowIndex].controls.associateName
if (event.keyCode !== 40 && event.keyCode !== 38 && value.length > 0) {
this.resourceAssignmentService.getAssociateNamesByName(value, this.id).pipe(takeUntil(this.onDestroy$)).subscribe((successRes: NamesList[]) => {
this.options = new Array<NamesList>();
successRes.forEach(element => {
this.options.push(element)
},
this.filteredOptions = this.SearchControl.valueChanges
.pipe(
startWith(''),
map(value => this._filter(value, this.options))
));
},
);
}
private _filter(value: string, options: NamesList[]): NamesList[] {
const filterValue = value.toLowerCase();
return options.filter(option => (option.fullName.toLowerCase().indexOf(filterValue) === 0) || option.fullName.toUpperCase().indexOf(filterValue) === 0);
}
`