html - 单击 Angular 6 中的图像时如何打开文件对话框

标签 html css angular6

我是 Angular 6 的新手。我知道这可能很愚蠢,但我没有得到任何解决方案。单击文本字段中的浏览图像时,我想打开文件对话框。这是我试过的。

<div class="boxed">
  <div class="input-group">
      <input id="spFile" class="form-control" name="spFile">
    <img src="../../../../../assets/images/maps/Browse.png" type= "file" width="40" height="40" style=" position: absolute; top: 1px; right: 1px" aria-hidden="true"/>
  </div>
</div>

如何让它轻松打开文件对话框?

最佳答案

这是一个比较老的问题,但是对于刚刚来这里的人,我有一个简单而流畅的解决方案。

为您的 <input> 添加一个 ID HTML 元素(在我的示例中为 FileSelectInputDialog)。

<input #FileSelectInputDialog type="file" multiple/>

在您的组件中使用 ViewChild 创建一个句柄:

import { ViewChild } from '@angular/core';

@ViewChild('FileSelectInputDialog') FileSelectInputDialog: ElementRef;

public OpenAddFilesDialog() {
    const e: HTMLElement = this.FileSelectInputDialog.nativeElement;
    e.click();
}

然后你可以添加 (click)="OpenAddFilesDialog()"指令到您的图像或任何其他 HTML 元素,例如,到一个常规按钮:

<button mat-icon-button class="BtnAddFiles" (click)="OpenAddFilesDialog()">
    <mat-icon>add_box</mat-icon>
</button>

关于html - 单击 Angular 6 中的图像时如何打开文件对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53138645/

相关文章:

Angular 服务装饰器提供了对延迟加载的根本影响

angular - 指定的模块不存在 Angular 6

html - 100% 宽度的页眉和页脚 bur 具有 100% 高度的固定内容

javascript - mmenu position & direction 属性从右边滑入

css - 如何以圆形显示 HTML5 视频标签?

html - 创建不跳转的 CSS 悬停样式

html - C、HTML 和 CGI

javascript - ReactJS/jQuery 获取 HTML

html - 粘性页脚 + float 元素

angular - Router-outlet 在 Routerlink 导航后没有更新