首页 > 解决方案 > Angular 从模板选择器中获取 $event

问题描述

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<input type="file" #myFile multiple />
<button (click)="onDelete(myFile.event)">DeleteFiles</button>

在上面myFile.event返回未定义!如何myFile通过单击按钮获取 $event,我只需要从用户选择中删除一个文件

标签: javascriptangulartypescript

解决方案


我认为您实际上不需要任何$event. 我认为您正在寻找这样的东西:

<input type="file" #myFile multiple>
<button (click)="onDelete(myFile.files)">DeleteFiles</button>

或者:

<input type="file" #myFile multiple>
<button (click)="onDelete(myFile.value)">DeleteFiles</button>

推荐阅读