簡體   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