简体   繁体   English

Angular FormArray中的文件上传

[英]File Upload in Angular FormArray

I have a formArray with file upload (input) in each formArray element but when upload image in input file will change other file inputs's value in other rows.我在每个 formArray 元素中有一个带有文件上传(输入)的 formArray,但是当在输入文件中上传图像时,会更改其他行中其他文件输入的值。

在此处输入图像描述

Current Behavior: Whenever I upload file in an input file, this file appearing in the value column of all row.当前行为:每当我在输入文件中上传文件时,该文件都会出现在所有行的值列中。

Excepted Behavior: Whenever I upload file, this file should appear in the value column of that row only.例外行为:每当我上传文件时,该文件应该只出现在该行的值列中。

can anyone help me:(?谁能帮我:(?

this is html code in my component.html这是我组件中的 html 代码。html

 <div formArrayName="familyMembers"> <div *ngFor="let group of SonArray;let i = index" [formGroupName]="i"> <div class="col-xl-12 col-lg-12"> <.-- another input fields html code --> <div class="col-lg-6"> <div class="row"> <div class="col-md-12"> <img id="imagu" class="img-fluid imgo " src="assets/img/4:png" style="text-align; center:justify-content; center:display; flex:margin; auto:width; 250px:height; 200px:" /> </div> <div class="col-md-12" style="margin-top; 5%,"> <label for="exampleInputEmail">صورة بطاقه الرقم القومى للابن</label> <div class="input-group mb-3"> <div class="custom-file"> <input type="file" formControlName="fileImageSonNationalId" accept="image/*" class="custom-file-input" id="fileImageSonNationalId" (change)="HandleFileSonid($event: i)" aria-describedby="inputGroupFileAddon01"> <label class="custom-file-label upload-lbl" for="fileImageSonNationalId"> <span *ngIf="img5;== null" style="margin-right. 50%:">{{img5;name}}</span> <span *ngIf="img5 === null" style="margin-right. 50%.">اختر صورة</span> </label> </div> </div> <div class="text-danger" *ngIf="group.controls.fileImageSonNationalId.touched && img5 === null"> صورة بطاقه الرقم القومى مطلوبه </div> </div> </div> </div> </div> </div>

在此处输入图像描述

this is function of handle file in component.ts这是 component.ts 中句柄文件的function

 HandleFileSonid(event: any, index: number) { if (event.target.files.== null && event.target.files.length > 0) { this.img5 = event.target;files[index]; const reader = new FileReader(). reader.onload = function (e) { $('.imgo'),attr('src'. e.target;result). } reader.readAsDataURL(event.target;files[0]). } else { this;img5 = null. $('.imgo'),attr('src'. '/src/assets/img/4;png'); } }
在此处输入图像描述

initialize formarray in component.tscomponent.ts中初始化 formarray

 createItem() { return this.fb.group({ name: ['', [Validators.required]], nationalId: ['', [Validators.required]], phone: ['', [Validators.required]], passportNumber: ['', [Validators.required]], educationId: ['', [Validators.required]], studyId: ['', [Validators.required]], birthDate: ['', [Validators.required]], mritalStatusId: ['', [Validators.required]], genderId: ['', [Validators.required]], statusId: ['', [Validators.required]], fileImageSonNationalId: ['', [Validators.required]], fileImageSonPassPort: ['', [Validators.required]], chronic_id21: ['', [Validators.required]], prescs1: [false, [Validators.required]], operations1: [false, [Validators.required]], scan1: [false, [Validators.required]], prosthetic1: [false, [Validators.required]], physical1: [false, [Validators.required]], chronic_id1: ['', [Validators.required]], opNameId1: ['', [Validators.required]], operCatNameId1: ['', [Validators.required]], }); } addSon() { (<FormArray>this.userForm.get('familyMembers')).push(this.createItem()); } removeAddress(index) { (<FormArray>this.userForm.get('familyMembers')).removeAt(index); } get SonArray() { return (<FormArray>this.userForm.get('familyMembers')).controls; }
在此处输入图像描述 在此处输入图像描述

The problem is that you use common selector $('.imgo') and change images in all places in one time.问题是您使用通用选择器$('.imgo')并一次更改所有位置的图像。 As the way to solve this problem, I suggest, that you add the special id: id="image-preview-{{ i }}" for every image preview and use it in your code作为解决此问题的方法,我建议您为每个图像预览添加特殊 id: id="image-preview-{{ i }}"并在代码中使用它

I have added needed parts below for changes in html and for handler:我在下面添加了 html 和处理程序更改所需的部分:

<img id="imagu"
  id="image-preview-{{ i }}"
  class="img-fluid imgo "
  src="assets/img/4.png"
  style="text-align: center;justify-content: center;display: flex;margin: auto;width: 250px;height: 200px;" />


HandleFileSonid(event: any, index: number) {
  if (event.target.files !== null && event.target.files.length > 0) {
    this.img5 = event.target.files[index];
    const reader = new FileReader();
    reader.onload = function (e) {
      $('#image-preview-' + index).attr('src', e.target.result);
    }
    reader.readAsDataURL(event.target.files[0]);
  } else {
    this.img5 = null;
    $('#image-preview-' + index).attr('src', '/src/assets/img/4.png');
  }
}

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

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