javascript - 无法在订阅内触发点击事件
问题描述
我在订阅内触发点击事件时遇到一些问题,这是代码。
replaceResource() {
// 'replace-file-input' is a file input html element
const element: HTMLElement = document.getElementById('replace-file-input') as HTMLElement;
// if I put "element.click()" here, eveything works fine.
// the next line is a subscription of the http request, I want to trigger the click event when I get the response back.
this.service.checkStatus(this.id).subscribe(
status => {
if (status) {
// here is the problem, the file input dialog should be opened, but nothing happened
element.click()
}
}
)
}
解决方案
尝试使用 ViewChild 而不是 document.getElementById('id')。ViewChild 装饰器提供“nativeElement”对象。也许这是一个问题。
推荐阅读
- javascript - 如何将 jQuery 库导入 Vue.js
- java - 无法让页面对象模型与 Appium、Serenity、Selenium、Java 一起使用
- python - 附加两个数据框(Python Pandas)
- python - 用于 csv 输出的字段的数据结构转换
- url - 不匹配任何可用的重载 (FileURLWithPath)
- typescript - 如何从 TypeScript 中的通用部分构建目标对象?
- c# - 更改程序集名称破坏了 xaml 命名空间
- c# - 在客户端 PC 上生成 SQL 数据库
- python - 如何更改全息视图中下拉/滑块更新的元素?
- c# - 用 shouldly 测试两个条件