设置了selectedIndex的角度步进器matStepperNext / matStepperPrevious不起作用

时间:2018-09-03 02:44:26

标签: angular angular-material angular6 angular-material-stepper

我正在使用matStepper,并且当我将selectedIndex设置为3时,我无法使用下一个和上一个导航。我可以单击水平步进器中的(1),然后照常使用下一个/上一个。所有表格均有效,单击(1)后,我可以使用1-7的下一个导航。

注意我有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://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;
  }
}

问题已通过堆栈闪电复制

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

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

2 个答案:

答案 0 :(得分:0)

快速破解:

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;
}

答案 1 :(得分:0)

要通过[linear]="true"使用此功能,请使用setTimeout延迟的0函数:

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

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