Cannot Find Control with Path Using ngIf on Recursive Angular Form

前端 未结 1 1845
半阙折子戏
半阙折子戏 2020-12-19 23:07

Here is the stackblitz for the code below. I am making a fairly complex deeply nested Angular form using FormArrays, and child components. For the most part, everything is w

相关标签:
1条回答
  • 2020-12-19 23:13

    Stackblitz demo

    Let's replace groupInstance for showConjunctor just to make it more obvious about what it's there for.

    You can do this in you app.component.hml:

    <app-group-control 
      #templateRef
      (remove)="_delete(i)"
      [formControlName]="i"
      [formLabel]="'Group '+ (i + 1) + ':'"
      [showConjunctor]="!((i + 1) % 2)">
    </app-group-control>
    

    I'm considering that i, in the above snippet, is the index of the current loop in *ngFor (like in the Stackblitz demo).

    Also, remove this part from the app.component.html:

    <div *ngIf="i > 0">
      <div [formGroupName]="i">
        <input type="text" formControlName="conjunctor">
      </div>
    </div>
    

    [UPDATE]: Per your comments, if you want to have the conjunctor in all of the nested Groups, you can set the @Input() showConjuntor to true inside the GroupControlComponent (Stackblitz demo):

    <ng-container formArrayName="groups">
      <app-group-control *ngFor="let s of _groupsFormArray?.controls; index as i"
                          (remove)="_deleteGroupFromArray(i)" 
                          [formControlName]="i"
                          [formLabel]="'Nested Group '+ (i + 1) + ':'"
                          [showConjunctor]="true">
      </app-group-control>
    </ng-container>
    
    0 讨论(0)
提交回复
热议问题