首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角FormArray索引未被传递到输入文件类型更改事件

角FormArray索引未被传递到输入文件类型更改事件
EN

Stack Overflow用户
提问于 2020-12-30 09:52:18
回答 1查看 371关注 0票数 1

我有一个角的ReactiveForms FormArray,它基本上呈现一个带有文件输入的扩展面板。我想传递FormArray的FormGroup索引,其中选择了一个文件。为此,我将索引(bankAccountIndex)作为附加参数传递给文件输入(change)事件。但是,值总是为零。如果我硬编码标记中的值,则在代码隐藏中获得该值,但在传递索引变量时不会得到。

下面是我的代码:

代码语言:javascript
复制
 <mat-accordion [togglePosition]="'before'">
              <ng-container *ngFor="let bankFormGroup of bankAccountsFormArray.controls; let bankAccountIndex=index">
                <ng-container [formGroupName]="bankAccountIndex">
                  <mat-expansion-panel>
                    <span>
                       <label for="file-upload" class="override-margin-bottom" [ngClass]="{'disabled-control': form.disabled}">
                           <fa-icon [icon]="faUpload" class="file-upload-icon" size="lg"></fa-icon>
                       </label>
                       <input type="file" id="file-upload" (change)="onHandleBankLogoUploadEvent($event.target.files, bankAccountIndex)"
                                style="display: none;" />
                       <input type="text" #bankLogo formControlName="bankLogoBase64Image" style="display: none;">
                       <img [src]="bankLogo.value" *ngIf="bankLogo.value" style="height: 30px;" />
                    </span>
                </mat-expansion-panel>
           </ng-container>
      </ng-container>
 </mat-accordion>

TypeScript文件输入更改事件处理程序:

代码语言:javascript
复制
onHandleBankLogoUploadEvent(files: FileList, bankAccountIndex: number) {
    ...
}

我的组件OnInit():

代码语言:javascript
复制
ngOnInit() {
    this.toastrService.overlayContainer = this.toastContainer;
    this.toastrService.clear();

    this.initializeForm();

    this.bankAccounts$ = this.firestore.collection<BankAccount>('bankAccounts')
      .valueChanges({ idField: 'docId' })
      .pipe(tap(result => {
        this.bankAccounts = result;
        this.distinctBankNames = result.map(bank => bank.bankName).filter((value, index, self) => self.indexOf(value) === index);
      }));

    this.bankAccounts$_subscription = this.bankAccounts$.subscribe(_ => {
      this.userContext$_subscription = this.appStoreService.userContext$.subscribe(context => {
        if (!!context) {
          this.toastrService.clear();
          this.selectedUser = context;
          this.selectedUserDocId = this.selectedUser.docId;
          this.initializeForm();
        } else {
          this.toastrService.info('Please select a specific family member.', null,
            {
              closeButton: true,
              disableTimeOut: true,
              enableHtml: false,
              positionClass: 'toast-top-center',
              easeTime: 100
            });
        }
      });
    });
  }

  initializeForm() {
    this.userFormGroup = this.formBuilder.group({
      isActive: [true, Validators.required],
      firstName: [this.selectedUser ? this.selectedUser.firstName : '', Validators.required],
      lastName: [this.selectedUser ? this.selectedUser.lastName : '', Validators.required],
      gender: [this.selectedUser ? this.selectedUser.gender : null, Validators.required],
      email: [this.selectedUser ? this.selectedUser.email : ''],
      pan: [this.selectedUser ? this.selectedUser.pan : ''],
      mobile: [this.selectedUser ? this.selectedUser.mobile : ''],
      aadharId: [this.selectedUser ? this.selectedUser.aadharId : ''],
      bankAccounts: this.selectedUser && this.selectedUser.bankAccountsRefs && this.selectedUser.bankAccountsRefs.length > 0 ?
        this.buildBankAccountsFormArray(this.selectedUser.bankAccountsRefs) : this.formBuilder.array([]),
      passports: this.selectedUser && this.selectedUser.passports && this.selectedUser.passports.length > 0 ?
        this.buildPassportsFormArray(this.selectedUser.passports) : this.formBuilder.array([]),
      webLogins: this.selectedUser && this.selectedUser.webLogins && this.selectedUser.webLogins.length > 0 ?
        this.buildWebLoginsFormArray(this.selectedUser.webLogins) : this.formBuilder.array([]),
    });
  }

  buildBankAccountsFormArray(bankAccountsRefs: DocumentReference[]): FormArray {
    if (bankAccountsRefs && bankAccountsRefs.length > 0) {
      const usersBankAccounts = this.bankAccounts.filter(acct => bankAccountsRefs.some(ref => ref.path.includes(acct.docId)));

      const bankAccountFormGroups = usersBankAccounts.map(acct => this.formBuilder.group({
        docId: [acct.docId],
        bankName: [acct.bankName, Validators.required],
        bankLogoBase64Image: [acct.bankLogoBase64Image],
        accountNumber: [acct.accountNumber, Validators.required],
        ifscCode: [acct.ifscCode, Validators.required],
        loginUserId: [acct.loginUserId],
        loginPassword: [acct.loginPassword],
        transactionPassword: [acct.transactionPassword],
        isActive: [acct.isActive, Validators.required],
      }))
      return this.formBuilder.array(bankAccountFormGroups);
    }
  }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-30 10:44:04

你的问题是身份证。您应该为每个输入分配唯一的id:

代码语言:javascript
复制
<label [for]="'file-upload-' + bankAccountIndex" class="override-margin-bottom" [ngClass]="{'disabled-control': form.disabled}">
   <fa-icon [icon]="faUpload" class="file-upload-icon" size="lg"></fa-icon>
</label>
<input type="file" [id]="'file-upload-' + bankAccountIndex" (change)="onHandleBankLogoUploadEvent($event.target.files, bankAccountIndex)" style="display: none;" />
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65505008

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档