首页 > 解决方案 > 无法在订阅内触发点击事件

问题描述

我在订阅内触发点击事件时遇到一些问题,这是代码。

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()
            }
        }
    )
}

标签: javascriptangulartypescript

解决方案


尝试使用 ViewChild 而不是 document.getElementById('id')。ViewChild 装饰器提供“nativeElement”对象。也许这是一个问题。


推荐阅读