Угловой степпер с установленным selectedIndex matStepperNext/matStepperPrevious не работает

Я использую matStepper и когда я устанавливаю selectedIndex на 3, я не могу перемещаться, используя next и previous. я могу щелкнуть (1) в горизонтальном степпере и затем использовать следующий / предыдущий как обычно. Все формы действительны, и я могу перемещаться, используя следующий от 1-7 после нажатия (1).

Обратите внимание, у меня есть this.selectedIndex = 3; жёстко

<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://stackru.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;
  }
}

Выпуск воспроизводится в stackblitz

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

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

4 ответа

Быстрый взлом:

HTML:

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

TS:

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

Установите индекс в component.js, а в степпере привяжите выбранный индекс к индексу

так:

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

Это работало с угловым 8

Чтобы это работало с [linear]="true", использовать setTimeout функционировать с 0 задержка:

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

Ссылка: https://github.com/angular/components/issues/8479

Я знаю, что это кажется глупым решением, но вы можете использовать stepper.next()

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

Другие вопросы по тегам