How to show Angular Material Stepper's matStepperNext button as enabled/disabled?

829 views Asked by At

In an Angular 11 project I have an Angular Material Stepper control. My first step contains a form much like this:

<mat-horizontal-stepper [linear]="true" #stepper>
    <mat-step [stepControl]="step.formGroup">
        <form [formGroup]="formGroup">
            <label for="code">Enter code</label>
            <input name="code" formControlName="code" required />
            <button matStepperNext>Continue</button>
        </form>
    </mat-step>
    <mat-step>
        <!-- step 2 -->
    </mat-step>
</mat-horizontal-stepper>

I would like to show the <button> that's decorated with matStepperNext as disabled when the form has errors and enabled when it doesn't using CSS classes. How can I get this done?

1

There are 1 answers

3
Mir entafaz Ali On BEST ANSWER
<button matStepperNext [ngClass]="{'disabled': formGroup.get('code').hasError(error)}">Continue</button>

CSS of the button:

button.disabled {
  pointer-events: none;
}

I would still suggest to handle it using disabled attribute on the button.