【发布时间】:2019-03-26 23:09:42
【问题描述】:
我有一个包含输入的 mat-form-field 和一个包含 mat-option 的 mat-autocomplete。输入具有 (blur) 事件,而 mat-option 具有 (onSelectionChange) 事件。
我遇到的问题是,当我选择一个项目时,会在 mat-option 的 onSelectionChange 事件之前调用模糊。如果下拉列表中不存在该值,则模糊事件的方法会清空输入。如果我删除输入的 (blur) 事件,则会调用 (onSelectionChange)。我需要在 (blur) 事件之前调用它。
经过一些研究,我发现我可以在 (blur) 的函数中使用 .setTimeOut,它允许在 (onSelectionChange) 的函数之后调用其主体,但是,通过此修复,远离输入的注意力会延迟如果输入的值错误,则清空输入。
这是我的 html 代码:
<mat-form-field [style.width.%]="fieldWidthPercentage">
<input matInput #tagsInput (blur)="checkIfMatch()">
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let item of filteredAutoCompleteItems | async"
(onSelectionChange)="selected(item)" [value]="item">
{{ item.name }}
</mat-option>
</mat-autocomplete>
</mat-form-field>
这是(模糊)事件的函数:
checkIfMatch() {
setTimeout(() => {
// . . .
}, 1000);
}
}
【问题讨论】:
标签: html angular angular-material