大佬教程收集整理的这篇文章主要介绍了如何在Angular 2中使用输入标记文件类型重置所选文件?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
<input type="file" placeholder="File Name" name="filename" (changE)="onChange($event)"> <button>Reset</button>
我想在Angular 2中重置所选文件.非常感谢帮助.如果您需要更多详细信息,请告诉我们.
附:
<input #myInput type="file" placeholder="File Name" name="filename" (changE)="onChange($event)">
然后在组件中,您需要从@ angular / core导入ViewChild:
import { ViewChild } from '@angular/core';
然后使用ViewChild访问模板的输入:
@ViewChild('myInput') myInputVariable: ElementRef;
现在您可以使用myInputVariable重置所选文件,因为它是对#myInput输入的引用,例如create button reset()将在按钮的click事件中调用:
reset() { console.log(this.myInputVariable.nativeElement.files); this.myInputVariable.nativeElement.value = ""; console.log(this.myInputVariable.nativeElement.files); }
第一个console.log将打印您选择的文件,第二个console.log将打印一个空数组,因为this.myInputVariable.nativeElement.value =“”;从输入中删除选定的文件.我们必须使用this.myInputVariable.nativeElement.value =“”;要重置输入的值,因为输入的FileList属性是只读的,所以不可能只从数组中删除项.这里工作Plunker.
以上是大佬教程为你收集整理的如何在Angular 2中使用输入标记文件类型重置所选文件?全部内容,希望文章能够帮你解决如何在Angular 2中使用输入标记文件类型重置所选文件?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。