2

目前,我有一个垂直垫步机。它使用 *ngFor 填充这些步骤。

<mat-step [stepControl]="firstFormGroup" *ngFor="let stream of selectedStreamList; let indexOfelement = index;">

所以,我想检测这个步进器的开始和这个步进器的结束以用作变量。目前,我正在使用变量作为计数器。我增加和减少它来检测垫步。但我想要一个好的解决方案。那么有没有人可以帮助我解决这个问题?

4

1 回答 1

2

每次步骤更改时,mat-stepper 都会发出一个 selectionChange 事件。您可以在您的打字稿文件中订阅这些更改并相应地处理......可能是这样的。

//app.component.ts

import { Component, ViewChild, AfterViewInit } from "@angular/core";
import { MatStepper } from "@angular/material/stepper";
import { StepperSelectionEvent } from "@angular/cdk/stepper";
import { pluck } from "rxjs/operators";

@Component({
  selector: "app-root",
  template: `
    <mat-vertical-stepper #stepper>
      <mat-step
        *ngFor="let stream of selectedStreamList; let indexOfelement = index"
      ></mat-step>
    </mat-vertical-stepper>
  `
})
export class AppComponent implements AfterViewInit {
  @ViewChild("stepper", { static: false }) private stepper: MatStepper;

  selectedStreamList = [1, 2, 3, 4];
  isAtStart: boolean;
  isAtEnd: boolean;

  constructor() {}

  ngAfterViewInit() {
    this.stepper.selectionChange
      .pipe(pluck("selectedIndex"))
      .subscribe((res: number) => {
        this.isAtStart = res === 0;
        this.isAtEnd = res === this.selectedStreamList.length - 1;
      });
  }
}
于 2020-04-01T18:00:27.580 回答