如何避免使用angular8在formcontrolname中重复相同的值

时间:2020-04-01 05:00:49

标签: angular angular-reactive-forms

我使用formArray循环值并选择一项,我们可以根据选择的一侧左右移动。在这里,我从右选择最后一项并向左移动,然后选择最后但是一个,然后将其移到左边。然后重复的值来了。我认为这是由于索引值不匹配。我无法纠正此问题。 谁能帮我解决这个问题。

演示: Working Demo

HTML:

<div class="card-body overflow-auto py-0" formArrayName="agentNotGroupView">
                  <div class="swap-list-item" *ngFor="let items of agentNotinView; let i = index" [formGroupName]="i"
                    [class.selected]="selected2.includes(items)">
                    <input formControlName="agentNotInViewValue" [readOnly]="true" (click)="onSelect(2, items)" [disabled] = "isReadOnly"/>
                  </div>
                </div>

TS:

    this.agentNotInViewArray = this.FB.array(
            this.agentNotinView.map(x => this.FB.group({
              agentNotInViewValue: this.FB.control(x.value)
            }))
          );
    
          this.agentGroupNotViewInfoForm = this.FB.group({
            agentNotGroupView: this.agentNotInViewArray
          })

move from one button to other:
moveFrom(fromListNumber: number) {
    const selected: MyInterface[] = this.getSelected(fromListNumber);
    if (selected.length === 0) {
      return;
    }

    const toListNumber: number = fromListNumber === 1 ? 2 : 1;

    const fromModel: MyInterface[] = this.getModel(fromListNumber);
    const fromFormArray: FormArray = this.getFormArray(fromListNumber);

    const toModel: MyInterface[] = this.getModel(toListNumber);
    const toFormArray: FormArray = this.getFormArray(toListNumber);

    // remove items and form groups    
    selected.forEach((item) => {
      const index: number = fromModel.indexOf(item);
      const formGroup: FormGroup = fromFormArray.controls[index] as FormGroup;

      // remove from model
      fromModel.splice(index, 1);
      // remove from from array
      fromFormArray.removeAt(index);

      // add to form array
      toFormArray.push(formGroup);
      // add item to model
      toModel.push(item);
    });
    // clear selected
    selected.length = 0;
  this.groupInfoForm();
  this.notGroupInfoForm();
    }

根据我的调试,item.value和items.id很好,但是formcontrolName给出了重复的值。 需要帮助以纠正这一点。

1 个答案:

答案 0 :(得分:0)

我能够通过在再次调用表单时添加if ans else条件来解决此问题, 就是这样。

TS:

moveFrom(fromListNumber: number) {
    const selected: MyInterface[] = this.getSelected(fromListNumber);
    if (selected.length === 0) {
      return;
    }

    const toListNumber: number = fromListNumber === 1 ? 2 : 1;

    const fromModel: MyInterface[] = this.getModel(fromListNumber);
    const fromFormArray: FormArray = this.getFormArray(fromListNumber);

    const toModel: MyInterface[] = this.getModel(toListNumber);
    const toFormArray: FormArray = this.getFormArray(toListNumber);

    // remove items and form groups    
    selected.forEach((item) => {
      const index: number = fromModel.indexOf(item);
      const formGroup: FormGroup = fromFormArray.controls[index] as FormGroup;

      // remove from model
      fromModel.splice(index, 1);
      // remove from from array
      fromFormArray.removeAt(index);

      // add to form array
      toFormArray.push(formGroup);
      // add item to model
      toModel.push(item);
    });
    // clear selected
    selected.length = 0;
    if(fromListNumber == 2) {
  this.groupInfoForm();
    } else if(fromListNumber == 1) {
  this.notGroupInfoForm();
    }
    }