2 回答

TA貢獻1829條經(jīng)驗 獲得超13個贊
如果您使用反應式表單,則只需調(diào)用reset()表單控件即可。
組件.html
<form [formGroup]="form">
<input type="file" multiple formControlName="files" />
<button type="button" (click)="clearFile()">
Delete
</button>
</form>
組件.ts
form: FormGroup;
ngOnInit() {
this.form = new FormGroup({
files: new FormControl('')
});
}
clearFile() {
this.form.get('files').reset();
}
演示: https: //stackblitz.com/edit/angular-huvm38

TA貢獻1804條經(jīng)驗 獲得超7個贊
您可以使用它ViewChild來訪問組件中的輸入。首先,您需要添加#someValue到輸入中,以便可以在組件中讀取它:
<input #myInput type="file" name="pj" id="pj" (change)="onFileChange($event)" multiple>
然后在您的組件中,您需要ViewChild從以下位置導入@angular/core:
import { ViewChild } from '@angular/core';
然后您可以使用ViewChild模板訪問輸入:
// ng 8 @ViewChild('myInput', {static: false}) myInput: ElementRef;
@ViewChild('myInput') myInput: ElementRef;
現(xiàn)在您可以使用myInput來重置所選文件,因為它是對輸入的引用#myInput,例如reset()將在click按鈕事件時調(diào)用的創(chuàng)建方法:
reset() {
console.log(this.myInput.nativeElement.files);
this.myInput.nativeElement.value = "";
console.log(this.myInput.nativeElement.files);
}
- 2 回答
- 0 關注
- 117 瀏覽
添加回答
舉報