如何停止javascript++或--条件

How to stop javascript ++ or - - condition

我有如下2个函数用于在网页中进行多步配置。

protected clickNext(event:any, config:any) : any{
    this.activeIndex++;
}

protected clickPrev(event:any, config:any) : any{
    this.activeIndex--;
}

按钮:

<div class="form-group text-center">
        <!-- <a class="btn" style="color: white" [disabled]="disabled"> Previous </a> -->
        <button type="submit" class="btn" style="color: white" [disabled]="prevBtn_disabled" (click)="clickPrev($event, _finalConfig)">Prev</button>
        <button type="submit" class="btn" style="color: white" [disabled]="nextBtn_disabled" (click)="clickNext($event, _finalConfig)">Next</button>
    </div>

当我单击下一步按钮时 clickNext 将触发,当我单击上一个按钮时 clickPrev 将被触发。 activeIndex 是传递给 html 的变量,用于确定要激活的步骤。这就是整个想法。如您所见,我得到 config 这是一个数组。我需要实现的是,一旦我到达数组中的最后一步或最后一个对象,我不需要为第一个对象执行 ++ 操作并单击 prev。不应该做--。抱歉,我对 Javascript 有点陌生。我怎样才能实现它?提前谢谢大家

您可以测试 this.activeIndex 是否已经等于 config 数组中第一项或最后一项的索引:

protected clickNext(event:any, config:any) : any{
    if (this.activeIndex < config.length - 1) {
        this.activeIndex++;
    }
}

protected clickPrev(event:any, config:any) : any{
    if (this.activeIndex > 0) {
        this.activeIndex--;
    }
} 

最好也禁用 NextPrev 按钮(如果已经在最后或第一步),以便用户一些迹象表明他们在一端或另一端。 (但我不能告诉你怎么做,因为你没有显示你的按钮。)