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

标签 javascript jquery angular typescript typescript2.0

我有如下 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/

相关文章:

javascript - 使用ajax加载谷歌地图

JavaScript-仅从数组中获取负数

javascript - 使用另一个属性选择属性值

javascript - 类型错误 : Cannot read property 'index' of null in Angular2

javascript - 下拉菜单的默认值

javascript - Konva 实时更新一个正确的位置

jQuery $.ajax 调用同步执行,而不是在调用 WCF 服务时并发执行

javascript - 如何在jquery中找到具有特定背景颜色的类?

angular - 通过 Angular 9 Reactive Forms 对一个输入使用多种模式的方法

angular - RxJS 如何按顺序发出 POST 请求,只有在前一个请求完成后才会触发新请求?