How to submit forms in Stepper in Angular 4 material?

Sivakumar Tadisetti

How to submit form data in the stepper of angular material. I am following the example from angular material https://material.angular.io/components/stepper/examples. I did lot of googling before asking this question, but not found any answer.

<mat-horizontal-stepper [linear]="isLinear" #stepper="matHorizontalStepper">
  <mat-step [stepControl]="firstFormGroup">
    <form [formGroup]="firstFormGroup">
      <ng-template matStepLabel>Fill out your name</ng-template>
      <mat-form-field>
        <input matInput placeholder="Last name, First name" formControlName="firstCtrl" required>
      </mat-form-field>
      <div>
        <button mat-button matStepperNext>Next</button>
      </div>
    </form>
  </mat-step>
  <mat-step [stepControl]="secondFormGroup">
    <form [formGroup]="secondFormGroup">
      <ng-template matStepLabel>Fill out your address</ng-template>
      <mat-form-field>
        <input matInput placeholder="Address" formControlName="secondCtrl" required>
      </mat-form-field>
      <div>
        <button mat-button matStepperPrevious>Back</button>
        <button mat-button matStepperNext>Next</button>
      </div>
    </form>
  </mat-step>
  <mat-step>
    <ng-template matStepLabel>Done</ng-template>
    You are now done.
    <div>
      <button mat-button matStepperPrevious>Back</button>
      <button mat-button (click)="stepper.reset()">Reset</button>
    </div>
  </mat-step>
</mat-horizontal-stepper>

I am done with filling two forms. But after that I am not getting how to get / submit the form data.

Thank you for you help... :-)

Abinesh Joyel

Give submit button and ngSubmit to form where you have forms inside Stepper

      <button mat-raised-button (click)="isLinear = true" id="toggle-linear">Enable linear mode</button>

<mat-horizontal-stepper [linear]="isLinear" #stepper="matHorizontalStepper">
  <mat-step [stepControl]="firstFormGroup">
    <form [formGroup]="firstFormGroup" (ngSubmit)="form1()" #formone="ngForm">
      <ng-template matStepLabel>Fill out your name</ng-template>
      <mat-form-field>
        <input matInput placeholder="Last name, First name" formControlName="firstCtrl" required>
      </mat-form-field>
      <div>
        <button type="button" mat-button matStepperNext>Next</button>
        <button type="submit" mat-button>submit</button>
      </div>
    </form>
  </mat-step>
  <mat-step [stepControl]="secondFormGroup">
    <form [formGroup]="secondFormGroup" (ngSubmit)="form2()" #formtwo="ngForm">
      <ng-template matStepLabel>Fill out your address</ng-template>
      <mat-form-field>
        <input matInput placeholder="Address" formControlName="secondCtrl" required>
      </mat-form-field>
      <div>
        <button type="button" mat-button matStepperPrevious>Back</button>
        <button type="button" mat-button matStepperNext>Next</button>
         <button type="submit" mat-button>submit</button>
      </div>
    </form>
  </mat-step>
  <mat-step>
    <ng-template matStepLabel>Done</ng-template>
    You are now done.
    <div>
      <button mat-button matStepperPrevious>Back</button>
      <button mat-button type="button" (click)="stepper.reset()">Reset</button>
      <button mat-button type="button" (click)="formone.ngSubmit.emit();formtwo.ngSubmit.emit()">
        submit
        </button>
    </div>
  </mat-step>
</mat-horizontal-stepper>

Component

form1(){
    console.log(this.firstFormGroup.value);
  }

  form2(){
    console.log(this.secondFormGroup.value);
  }

Working Demo

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Angular 4 & Material Stepper

How to change state icon in Angular Material stepper?

How to target angular material stepper padding with CSS?

How to add tooltip to Angular material stepper

How to make CSS change in Angular Material Stepper?

How to reset a 'Material Angular Stepper' Step?

How to stop user moving to next step until finish the current step using Angular Material Stepper(Angular 4)

Angular material stepper hover

Angular Material Stepper 'Paginate'

Angular Material - How to display matTooltip for angular stepper when the steps are disabled

Angular - How to Validate each step in Angular Material stepper

How to load steps dynamically using Angular material stepper

How to change the angular material mat stepper label color?

How to change state icon in Angular material stepper dynamically created component?

Angular Material Stepper - how to dynamically create components to load into steps

How to change angular material stepper step numbers to any icon or text?

How to change color of angular material stepper step icons

How to set an animation to Nebular Stepper with Angular as Material does it

How to remove default focus on stepper header in angular material?

How to replace next button with mat-chip in Angular Material stepper?

How to customize Angular Material stepper component showing label at the top?

Angular Material Stepper with Routing to components

Angular Material Stepper not working on StackBlitz

wrong icon in material angular stepper

Angular material stepper remove numbers

Angular Material: How to set each mat-horizontal-stepper stepper icon with different background color in TS

Angular Material Stepper causes mat-formfield to validate for dynamic forms when returning to an older step

How to set material stepper to open second item by default and set the icon color?(Angular Material)

Angular material 2 stepper - next step