【发布时间】:2018-09-07 15:18:03
【问题描述】:
我正在尝试让步进器根据用户离开的位置转到设置的问题。我正在使用 selectedIndex,并将其设置为我从服务器检索到的数字。
HTML:
<mat-horizontal-stepper class="my-stepper" [linear]="true" #stepper="matHorizontalStepper" [selectedIndex]="currentQuestion">
<mat-step *ngFor="let question of currentVideoCount">
<ng-template matStepLabel>Question {{question}}</ng-template>
</mat-step>
</mat-horizontal-stepper>
打字稿:
public currentVideoCount: number[];
public currentQuestion: number;
ngAfterViewInit() {
this.programId.subscribe((programId) => {
this.evalService.getCurrentVideoCount(programId).subscribe(data => {
this.currentVideoCount = data.videoCount;
this.currentQuestion = data.question;
});
})
}
这不起作用。如果我这样做,我会收到此错误。
ERROR Error: cdkStepper: Cannot assign out-of-bounds value to `selectedIndex`.
但是,如果我只是将 html 更改为不使用 currentQuestion,而只使用一个数字或我定义为 1、2 等的变量,它确实有效。如果我只是将 currentQuestion 放在 html 标记中,它会给我正确的数字。如果我在任何地方记录它,它会给我正确的号码。但是步进器本身不会使用数字,只有以这种方式给它数字。如何让它为 selectedIndex 使用 currentQuestion?我认为它出了问题,因为我在订阅中定义它的方式,但我不知道如何解决这个问题。
编辑:如果我将 currentQuestion 初始化为我期望 data.question 的数字,它会起作用,但如果我将它初始化为其他东西则不会。显然不是我想要的,但仍然很有趣。
编辑:如果我将 selectedIndex 默认设置为超出范围,例如 3 个项目的数组中的 300 个,我不会收到超出范围的错误,它只会使整个步进器变灰。
【问题讨论】: