我有如下 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,它是一个数组。我需要实现的是,一旦到达最后一步或数组中的最后一个对象,如果我位于第一个对象并单击上一个,则不需要执行++ 相同的操作。不应该做--。抱歉,我对 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--;
}
}
如果已经处于最后一步或第一步,最好也禁用您的下一步或上一步按钮,以便用户知道他们处于其中一个结束或其他。 (但我无法告诉您如何执行此操作,因为您没有显示按钮。)
关于javascript - 如何停止 javascript++ 或 - - 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42824028/