Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have followed https://devblog.dymel.pl/2016/09/02/upload-file-image-angular2-aspnetcore/ to upload single file and its working fine.

Now I want to upload dynamic no of files based on the no of elements in an array documentTypes[] .

My HTML:

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

My TS:

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

Submit function:

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
  }
};

Any suggestion will be appreciated.

like image 537
Adrita Sharma Avatar asked Mar 10 '26 22:03

Adrita Sharma


1 Answers

@ViewChild decorator is only for one child. To get array of children you need to use @ViewChildren

like image 116
be4code Avatar answered Mar 13 '26 16:03

be4code



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!