1

I am looping through the controls for my Angular form. However, the controls are coming from the server, and every form will be different. However, I want to add a submit button to the last control/material stepper. How would I do this?

<ng-container *ngFor="let control of formControls; let i = index">
        <div class="row">
          <div class="col">

            <mat-step *ngIf="['text', 'number'].includes(control.type)" [label]="control.name" class="mb-3">
              <mat-form-field class="mt-3">
                <input matInput [formControlName]="control.name" [placeholder]="control.name" [type]="control.type" class="mt-3">
              </mat-form-field>

              <div>
                <button mat-button matStepperPrevious uiKitButton="secondary" class="mr-1" type="button">Back</button>
                <button mat-button matStepperNext uiKitButton="primary" type="button">Next</button>
              </div>

            </mat-step>

            
            <mat-step *ngIf="['select'].includes(control.type)" [label]="control.name">
              <mat-form-field class="mt-3">
                <mat-select [formControlName]="control.name" [placeholder]="control.name">
                  <mat-option *ngFor="let option of control.options" [value]="option.name">{{ option.name }}
                  </mat-option>
                </mat-select>
              </mat-form-field>
              
              <div>
                <button mat-button matStepperPrevious uiKitButton="secondary" class="mr-1" type="button">Back</button>
                <button mat-button matStepperNext uiKitButton="primary" type="button">Next</button>
              </div>
              
            </mat-step>
Paul Han
  • 31
  • 1

2 Answers2

1

NgFor supports exposes more values than index. Your code could look as similar to this:

<ng-container *ngFor="let control of formControls; let i = index; let isLast = last">
  <!-- your code -->

  <ng-container *ngIf="isLast">
    <button type="submit">Submit form</button>
  </ng-container>
</ng-container>

Ref

Paweł Jacewicz
  • 134
  • 1
  • 7
0

You can check the last index of your formControls like this:

    <div *ngIf="i == (formControls.length - 1)">
      <button>
        Submit
      </button>
    </div>
Akif
  • 7,098
  • 7
  • 27
  • 53