【发布时间】:2017-11-15 04:43:59
【问题描述】:
在我的应用程序中,我有一个全局搜索字段,用于过滤列表中的数据,列表将有多个列。从其他组件设置过滤器值(设置为输入值)它正在发生,但我必须触发输入上的手动键盘事件(输入键)操作。
我尝试使用 viewChild 装饰器。
component.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);
使用它我可以设置值并获得焦点,但键盘事件没有触发。
【问题讨论】:
-
通过将事件更改为
const event = new KeyboardEvent("keypress", { key: '13' } );它对我有用。假设您在视图已经实例化时执行您的操作。您能否为您的代码提供更多细节或更好的stacklitz? :)
标签: javascript angular