2017-09-13 124 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