【问题标题】:Angular Reactive Forms - FormArray and pacthValueAngular Reactive Forms - FormArray 和 pacthValue
【发布时间】:2020-07-24 16:19:10
【问题描述】:

我在使用带有 formArray 的 patchValue 时遇到问题。 我有一个使用<ul><li> 进行的自定义选择,单击一个选项会执行一个方法,该方法应该修补在<input type="hidden" /> 中选择的选项的ID,它将包含formarray 中的属性值。

这就是表单的构建方式:

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

【问题讨论】:

  • 您在哪里以及如何使用patchValue?你能发布那个方法吗?
  • 问题不清楚。如果您还可以创建一个堆栈闪电战,那就更好了
  • 注意:pacthValue 不向数组添加新元素,您需要调用您的函数 addProfile 或函数以在生成 pacthValue 之前删除数组的元素
  • 抱歉,我更新了问题,并描述了我遇到的确切问题
  • 什么是profileTypes?看起来 roleForm.profiles 与任何内容都不同步

标签: javascript angular angular-reactive-forms reactive-forms formarray


【解决方案1】:

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

<div class="col-md-8" formArrayName="profiles">
  <div class="row" *ngFor="let profile of getProfiles(); let outterI = index"> <!-- declared here -->
    <div class="col-md-6" [formGroupName]="outterI">
      <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">
            <!-- and here -->
            <li *ngFor="let profileType of profileTypes; index as innerI" 
                class="list-group-item rounded-0 pointer" 
                [ngClass]="{'selected bg-primary text-white font-weight-bold': this.selectedProfile === profileType.profileId}" 
                (click)="selectProfile(profileType.profileId, outterI)">
              {{profileType.profileDescription}}
            </li>
          </ul>
          <input formControlName="perfil" type="text" />
        </div>
      </div>
    </div>
  </div>
</div>

使用ngFor 时不需要声明索引变量,但如果您不使用它,因为在此示例中您似乎没有使用innerI

【讨论】:

  • 谢谢@bryan60,我可以解决我的问题,让我注意到我在处理索引时犯的愚蠢错误。
猜你喜欢
  • 2022-12-27
  • 2018-10-13
  • 2018-09-06
  • 2019-01-04
  • 1970-01-01
  • 2019-01-31
  • 2018-12-22
  • 2018-12-22
  • 2022-01-25
相关资源
最近更新 更多