Angular formgroup, removeat(0) 删除 formgroup 中的第二个而不是第一个控件

Angular formgroup, removeat(0) removing 2nd not first control in formgroup

我有一个包含 x 个控件的格式数组和一个删除它们的相应按钮,但是当所有其他按钮工作时,第一个控件的按钮删除第二个而不是第一个。

这是我的模板代码,如果您按 ctrl-f color

,您可以找到删除控件的按钮
<form [formGroup]="myForm" novalidate (ngSubmit)="submitModal(myForm)">
    <div class="form-group">
        <label>Routine Name</label>
        <input type="text" formControlName="routineName" value="{{routine.routineName}}">

        <small *ngIf="!myForm.controls.routineName.valid" class="text-danger">
            Name is required (minimum 6 characters).
        </small>
    </div>

    <ion-card formArrayName="subroutines">
        <ion-card-content *ngFor="let rout of myForm.controls.subroutines.controls; let i=index">
            <ion-row [formGroupName]="i">
                <ion-col>
                    <ion-label>Set {{i+1}}</ion-label>
                </ion-col>
                <ion-col>
                    <ion-input type="text" formControlName="setExercise" placeholder="Exercise" value="{{routine.sets[i].setExercise}}"></ion-input>
                    <small [hidden]="myForm.controls.subroutines.controls[i].controls.setExercise.valid">
                        Set Exercise is required
                    </small>
                </ion-col>
                <ion-col>
                    <ion-input type="number" formControlName="repAmount" min="0" value="{{routine.sets[i].repAmount}}"></ion-input>
                </ion-col>
                <ion-col>
                    <ion-input type="number" formControlName="setTime" min="0" value="{{routine.sets[i].setTime}}"></ion-input>
                </ion-col>  
                <ion-col>
                    <button ion-button color="danger" *ngIf="myForm.controls.subroutines.controls.length > 1" (click)="removeSubRoutine(i)"> <ion-icon name="trash"></ion-icon> </button>
                </ion-col>          
            </ion-row>
        </ion-card-content>
    </ion-card>

    <div class="margin-20" *ngIf="myForm.controls.subroutines.controls.length < 10">
      <a (click)="addSubRoutine(true)" style="cursor: default">
        Add another set +
      </a>
    </div>
    <button ion-button type="submit" [disabled]="!myForm.valid">Submit</button>
</form>

这是我的打字稿,它在构造函数中创建表单,最后一个函数是在 i

处删除控制的函数
@IonicPage()
@Component({
  selector: 'page-routine',
  templateUrl: 'routine.html',
})
export class RoutinePage {
  public myForm: FormGroup;
  edit: boolean;
  routine: RoutineModel;

  constructor(public viewCtrl: ViewController, public navParams: NavParams, private _fb: FormBuilder) {
    let rout = new SubRoutine("",0,0);
    this.routine = new RoutineModel("", [rout]);

    this.myForm = this._fb.group({
      routineName: ['', [Validators.required, Validators.minLength(6)]],
      subroutines: this._fb.array([
        this.initSubRoutine(),
        ]),
      rid: ['']
    });

    if(navParams.get('routine') != null)
    {
      this.edit = navParams.get('edit');
      this.routine = navParams.get('routine');

      this.myForm.patchValue({
        routineName: this.routine.routineName,
        rid: this.routine.rid
      })

      for(var i = 0; i < this.routine.sets.length-1; i++)
      {
        this.addSubRoutine(false);
      }
    }
  }

  initSubRoutine(){
    return this._fb.group({
      setExercise:['', Validators.required],
      repAmount:[''],
      setTime:['']
    });
  }
  addSubRoutine(addSub){
    if (addSub == true)
    {
      let rout = new SubRoutine("default",0,0);
      this.routine.sets.push(rout);
    }
    const control = <FormArray>this.myForm.controls['subroutines'];
    control.push(this.initSubRoutine());
  }

  removeSubRoutine(i: number){
    console.log(i);
    const control = <FormArray>this.myForm.controls['subroutines'];
    control.removeAt(i);
  }

原来我的 this.routine 子例程数组和我的表单数组大小不同,因为我在构造函数中进行了初始化,解决了这个问题并且它完美地工作。