繁体   English   中英

如何在 Angular 中使用多个 ViewChild 元素 (2/4)

[英]How to use multiple ViewChild element in Angular (2/4)

我已经按照https://devblog.dymel.pl/2016/09/02/upload-file-image-angular2-aspnetcore/上传单个文件并且它工作正常。

现在我想根据数组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
  }
};

任何建议将不胜感激。

@ViewChild装饰器仅适用于一个孩子。 要获得孩子的数组,您需要使用@ViewChildren

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM