Angular反应形式-FormArray和pacthValue

问题描述 投票:0回答:1

我在将patchValue与formArray一起使用时遇到麻烦。我有一个用<ul><li>进行的自定义选择,单击选项将执行一个方法,该方法应修补<input type="hidden" />中所选选项的ID,该ID将包含表单数组中的属性值。 >

这是表单的构建方式:

  this.roleForm = this.fb.group({
    profiles: this.fb.array([ this.fb.group({perfil: '', accion: ''}) ])
  })

这是在数组中添加新配置文件的方法:

  addProfile() {
    const prof = this.roleForm.get('profiles') as FormArray;
    prof.push(this.fb.group({
      perfil: [''],
      accion: ['']
    }))
  }

这是html:

<div class="col-md-8" formArrayName="profiles">
  <div class="row" *ngFor="let profile of getProfiles(); let i = index">
    <div class="col-md-6" [formGroupName]="i">
      <div class="col-12">
        <div class="card card-primary bg-white">
          <div class="card-header h4">
            Perfil
          </div>
          <ul class="list-group list-group-flush register h5 scroll-list">
            <li *ngFor="let profileType of profileTypes; index as i" 
                class="list-group-item rounded-0 pointer" 
                [ngClass]="{'selected bg-primary text-white font-weight-bold': this.selectedProfile === profileType.profileId}" 
                (click)="selectProfile(profileType.profileId, i)">
              {{profileType.profileDescription}}
            </li>
          </ul>
          <input formControlName="perfil" type="text" />
        </div>
      </div>
    </div>
  </div>
</div>

编辑:

我必须在此方法中使用patchValue来设置具有相同索引的输入中的选定值。但是我无法使at()方法起作用,这总是返回一个未定义的信息:

selectProfile(profileType.profileId, i) {
 let x = (<FormArray>this.roleForm.get('profiles')).at(i);
 console.log(x)
}

我在使用formArray的patchValue时遇到麻烦。我用[

  • ,单击选项进行了自定义选择,单击选项执行的方法应修补在...
]]
javascript angular angular-reactive-forms reactive-forms formarray
1个回答
0
投票

我猜您的问题是模板变量i的重复声明:

© www.soinside.com 2019 - 2024. All rights reserved.