を使用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()
呼び出されるcreateメソッドを使用した入力への参照であるためですclick
。
reset() {
console.log(this.myInputVariable.nativeElement.files);
this.myInputVariable.nativeElement.value = "";
console.log(this.myInputVariable.nativeElement.files);
}
最初console.log
に選択したファイルを印刷し、2番目console.log
にthis.myInputVariable.nativeElement.value = "";
選択したファイルを入力から削除するため、空の配列を印刷します。this.myInputVariable.nativeElement.value = "";
入力のFileList
属性は読み取り専用であるため、を使用して入力の値をリセットする必要があります。そのため、配列からアイテムを削除するだけでは不可能です。これがPlunkerの動作です。