18

Im using a matStepper and when i set the selectedIndex to 3 i cannot navigate using next and previous. i can click the (1) in the horizontal stepper and then use the next/prev as usual. All the forms are valid and i can navigate using next from 1-7 after clicking (1).

Note i have this.selectedIndex = 3; hardcoded

<mat-horizontal-stepper #stepper
                        [linear]="true"
                        [selectedIndex]="this.selectedIndex"
                        (selectionChange)="selectionChange(stepper)">

  <mat-step [stepControl]="form1">
    <app-observer-information></app-observer-information>
  </mat-step>
...
  <mat-step [stepControl]="form7">
    <app-observer-agreement></app-observer-agreement>
  </mat-step>

</mat-horizontal-stepper>



export class ObservationStatementStepperComponent implements OnInit {

  @ViewChild('ObserverInformationComponent') public component1: ObserverInformationComponent;
  ..
  @ViewChild('ObserverAgreementComponent') public component7: ObserverAgreementComponent;

  public selectedIndex: number;

  constructor(private sorDataService: SorDataService) {

    this.selectedIndex = 3; // Number(sorDataService.selectedIndex);
  }

  public ngOnInit() {
  }

  public selectionChange(stepper) {

    this.sorDataService.synchronizeStepper(stepper.selectedIndex + 1);
  }

  /**
   * @see https://stackoverflow.com/questions/48498966/angular-material-stepper-component-for-each-step
   */
  get form1() {
    return this.component1 ? this.component1.form : null;
  }
  ...
  get form7() {
    return this.component7 ? this.component7.form : null;
  }
}

Issue is reproduced in stackblitz

<mat-horizontal-stepper linear #stepper [selectedIndex]="1">

https://stackblitz.com/edit/angular-syml71?file=app/create-profile.component.html

Ricardo Saracino
  • 1,345
  • 2
  • 16
  • 37

4 Answers4

18

Quick hack:

HTML:

<mat-vertical-stepper [linear]="true" #stepper>

TS:

 @ViewChild('stepper')
  stepper: MatStepper;
 
 nextClick(): void {
    this.stepper.linear = false;
    this.stepper.selectedIndex = ...;
    setTimeout(() => {
       this.stepper.linear = true;
    });
}

Edit: had to add "setTimeout" since it's not working without it anymore. Seems like a crazy hack which I hate, but no other choice unfortunately.

Oleg
  • 594
  • 8
  • 9
  • 2
    This didn't work for me with Angular/Material 8+ . Seems like there is more discussion about it not working here - https://github.com/angular/components/issues/8479 – chrismarx Dec 11 '19 at 16:07
  • Yep, doesn't work with the v9, but setTimeout to the rescue. IMHO the Material team should make it working without all kind of crazy hacks. – Oleg May 01 '20 at 05:56
0

To make this work with [linear]="true", use a setTimeout function with 0 delay:

setTimeout(() => {
  this.stepper.selectedIndex = ...;
});

Reference: https://github.com/angular/components/issues/8479#issuecomment-444063732

double-beep
  • 5,031
  • 17
  • 33
  • 41
phanf
  • 661
  • 1
  • 11
  • 16
0

I know it seems dumb solution, but you can use stepper.next()

 @ViewChild('stepper')  stepper: MatStepper;
 selectedStepNumber = 2;
 ....
  ngAfterViewInit() {
      setTimeout(() => {
        for (let i = 0; i < this.selectedStepNumber; i++) {
          this.stepper.next();
        }
      }, 0);
    }

Armen Stepanyan
  • 1,618
  • 13
  • 29
0

Set the index in the component.js, and in the stepper bind the selected index with the index

like this:

       <mat-horizontal-stepper #stepper [selectedIndex]="index">
        <mat-step>
        </mat-step>
        <mat-step>
        </mat-step>
      </mat-horizontal-stepper>

This worked with angular 8