2017-09-13 123 views
1

我跟着https://devblog.dymel.pl/2016/09/02/upload-file-image-angular2-aspnetcore/上傳單個文件並且工作正常。如何在Angular中使用ViewChild上傳多個文件(2/4)

現在我想上傳基於數組中元素數量的動態數量documentTypes[]

我的HTML:

<div *ngFor="let item of documentTypes ;let i=index"> 
    <input #fileInput[i] type="file" #select name="document[{{i}}]" /> 
</div> 

我的TS:

@ViewChild("fileInput") fileInput: any[]; 

提交功能:

onSubmit = function (docs) { 
    for (var i = 0; i < this.documentTypes.length; i++) { 
     let fi = this.fileInput[i].nativeElement; 
     //This is not working. Here fileInput is undefined 
    } 
} 

任何建議將不勝感激。

回答

1

@ViewChild裝飾者是隻爲一個孩子。要獲得需要使用的兒童陣列@ViewChildren

+0

非常感謝。它解決了我的問題:) –

+0

嘗試對其他人有所幫助。這是stackoverflow的想法。你需要分享你的知識。 – be4code