在我的应用程序中,我有一个全局搜索字段,用于过滤列表中的数据,列表将有多个列。从其他组件设置过滤器值(设置为输入值)它正在发生,但我必须触发输入上的手动键盘事件(输入键)操作。
我尝试使用 viewChild 装饰器。
组件.html
<input #gb type="text" placeholder="Global search.." class="changeListComponent_inputSearch" [(ngModel)]="jiraRef" />
组件.ts
@ViewChild('gb') gb:ElementRef;
this.jiraRef = jiraRef;
const event = new KeyboardEvent("keypress",{ "which ": "13"});
this.gb.nativeElement.focus();
this.gb.nativeElement.dispatchEvent(event);
使用它我可以设置值并获得焦点,但键盘事件没有触发。
你可以简单地使用
解决方案基于此答案 Get which key pressed from (keypress) angular2