gpt4 book ai didi

Angular 7 - 使 div 上的 keydown 事件不起作用

转载 作者:行者123 更新时间:2023-12-02 19:50:49 24 4
gpt4 key购买 nike

我有带有搜索表单的 Angular 7 应用程序。当输入表单时,我会过滤输出并将输出显示为 div 列表。当使用(单击)事件时,我可以选择并触发功能,但是当尝试使用(keydown)事件时,它不起作用。

<input type="text" class="form-control" 
id="licensePlate"
aria-describedby="licensePlate"
name="licensePlate" #licensePlate="ngModel"
required minlength="2" [(ngModel)]="carModel" [ngbTypeahead]="carAutoComplete"
[inputFormatter]="CarFormatter"
[resultTemplate]="carTemplate">

使用鼠标:

<ng-template #carTemplate let-r="result" let-t="term">
<div (click)="searchCar(r.id)">{{ r.licensePlate }} - {{ r.make }} {{ r.carModel }}</div>
</ng-template>

无法使用键盘

<ng-template #carTemplate let-r="result" let-t="term">
<div (keydown)="keyDownFunction($event)" tabindex="0">{{ r.licensePlate }} - {{ r.make }} {{ r.carModel }}</div>
</ng-template>

这是我的 TS 代码:

carAutoComplete = (text$: Observable<string>) =>
text$.pipe(
debounceTime(200),
distinctUntilChanged(),
map(term => term.length < 2 ? []
: this.carsList.filter(v => v.licensePlate.toLowerCase().indexOf(term.toLowerCase()) > -1).slice(0, 10))
)
CarFormatter = (x: {licensePlate: string}) => x.licensePlate;

keyDownFunction(event) {
console.log('Clicked keyboard:', event);
}

searchCar(id: number) {
this.searchSelected = true;
this.router.navigate(['car', id]);
}

我希望能够仅使用鼠标和键盘来选择汽车。

最佳答案

为了在 divp 等元素上发生 keydown 事件,您必须使用 contenteditable属性。

所以你会得到这样的东西:

<ng-template #carTemplate let-r="result" let-t="term">
<div contenteditable="true" (keydown)="keyDownFunction($event)" tabindex="0">{{ r.licensePlate }} - {{ r.make }} {{ r.carModel }}</div>
</ng-template>

关于Angular 7 - 使 div 上的 keydown 事件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58159586/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com