Angular 4 & Material Stepper

Ben jamin picture Ben jamin · Sep 27, 2017 · Viewed 37.3k times · Source

Is it possible to reset (or just jump to the first step) inside of a stepper? It is not documented in the docs, but is it possible to do so? It is stated in the docs that the stepper is built on "CDK Stepper" (link?), but I can't find any examples which lead me to my goal.

Answer

Ben jamin picture Ben jamin · Sep 27, 2017

Ok, it seems I found a solution (but it is not stated anywhere on the API).

  1. Add a reference to the stepper, then add ViewChild with the reference in your component typescript file.
  2. Create a method to change the index of the stepper.

HTML:

<mat-horizontal-stepper [linear]="true" #stepper>
    <!-- Content here -->
</mat-horizontal-stepper>

TS:

import { Component, ViewChild } from '@angular/core';
@Component({
    // ...
})
export class AppComponent {
    @ViewChild('stepper') stepper;

    /**
     * Changes the step to the index specified
     * @param {number} index The index of the step
     */
    changeStep(index: number) {
        this.stepper.selectedIndex = index;
    }
}