를 사용 ViewChild
하여 구성 요소의 입력에 액세스 할 수 있습니다 . 먼저, #someValue
컴포넌트에서 읽을 수 있도록 입력 에 추가 해야합니다.
<input #myInput type="file" placeholder="File Name" name="filename" (change)="onChange($event)">
그런 다음 구성 요소는 가져와야 ViewChild
에서 @angular/core
:
import { ViewChild } from '@angular/core';
그런 다음 ViewChild
템플릿에서 입력에 액세스하는 데 사용 합니다.
@ViewChild('myInput')
myInputVariable: ElementRef;
이제를 사용 myInputVariable
하여 입력에 대한 참조이므로 선택한 파일을 재설정하는 데 사용할 수 있습니다 ( #myInput
예 : 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 = "";
input의 FileList
속성이 readonly 이기 때문에 input의 값을 재설정하는 데 사용해야 하므로 배열에서 항목을 제거하는 것은 불가능합니다. 여기 Plunker 가 작동 합니다 .