Programmatic Control of Mat-Horizontal-Stepper in Angular Material: A Comprehensive Guide

Dec 03, 2025 · Programming · 13 views · 7.8

Keywords: Angular | Angular Material | MatStepper | programmatic control | step navigation

Abstract: This article explores methods for programmatically controlling the steps of a <code>&lt;mat-horizontal-stepper&gt;</code> in Angular Material. By leveraging the <code>selectedIndex</code> property and public methods <code>next()</code> and <code>previous()</code>, developers can move navigation buttons outside the stepper or control steps via code. The guide covers implementation using event binding and <code>ViewChild</code> decorator, with code examples and best practices for enhanced user experience.

Introduction

In Angular application development, Angular Material's &lt;mat-horizontal-stepper&gt; component is commonly used for creating multi-step forms or wizard interfaces. By default, each step includes navigation buttons, but sometimes it is necessary to move these buttons outside the component or control step transitions programmatically. This article delves into methods for achieving programmatic control of stepper steps.

Core Concepts and API

The MatStepper component provides the selectedIndex property for getting or setting the currently selected step index. Additionally, it exposes public methods next() and previous(), allowing programmatic movement forward or backward through steps.

Implementation Method One: Passing Reference via Event Binding

In the template, add a reference identifier to the stepper, such as #stepper, and pass this reference to component methods.

&lt;mat-horizontal-stepper #stepper&gt;
    &lt;mat-step&gt;Step 1&lt;/mat-step&gt;
    &lt;mat-step&gt;Step 2&lt;/mat-step&gt;
    &lt;mat-step&gt;Step 3&lt;/mat-step&gt;
&lt;/mat-horizontal-stepper&gt;
&lt;div&gt;
   &lt;button (click)="goBack(stepper)" type="button"&gt;Back&lt;/button&gt;
   &lt;button (click)="goForward(stepper)" type="button"&gt;Next&lt;/button&gt;
&lt;/div&gt;

In the TypeScript component, define the corresponding methods:

import { MatStepper } from '@angular/material/stepper';

goBack(stepper: MatStepper) {
    stepper.previous();
}

goForward(stepper: MatStepper) {
    stepper.next();
}

Implementation Method Two: Using ViewChild Decorator

Another approach is to use Angular's ViewChild decorator to directly obtain a reference to the stepper component.

@ViewChild('stepper') private myStepper: MatStepper;

goBack() {
    this.myStepper.previous();
}

goForward() {
    this.myStepper.next();
}

In the template, buttons do not need to pass the reference:

&lt;button (click)="goBack()" type="button"&gt;Back&lt;/button&gt;
&lt;button (click)="goForward()" type="button"&gt;Next&lt;/button&gt;

Advanced Techniques: Enabling and Disabling Buttons

To enhance user experience, navigation buttons can be dynamically enabled or disabled based on the current step. For example, disable the "Back" button when on the first step, and disable the "Next" button when on the last step.

&lt;button (click)="goBack(stepper)" type="button" 
        [disabled]="stepper.selectedIndex === 0"&gt;Back&lt;/button&gt;
&lt;button (click)="goForward(stepper)" type="button" 
        [disabled]="stepper.selectedIndex === stepper._steps.length - 1"&gt;Next&lt;/button&gt;

Conclusion

By programmatically controlling the MatStepper, developers can flexibly design stepper interfaces, separating navigation logic from business code, thereby improving application maintainability and user experience. The methods discussed in this article are applicable to Angular 4 and above, based on Angular Material best practices.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.