簡體   English   中英

遞減計數器無法正常工作

[英]Decrement counter not working as expected with respect to increment counter-angular js

我試圖執行以4個數字/年為單位的增量和減量功能,其中導航應限制在4年的數組限制內。

我可以增加每次單擊遞增按鈕的次數,但不能以相同的相反順序遞減。單擊遞增按鈕后,遞減按鈕將被禁用,僅當我到達遞增/遞減的最后一個索引時才應如此反之亦然

操場在這里

 $scope.navigateYear=function(year, mode) {
       const index = $scope.yearArray.indexOf(year);
         if (index >= 0 && index < $scope.yearArray.length - 1 && mode === 'plus') {
             this.year = $scope.yearArray[index + 1];
        }else{
          $scope.isPlusButtondisabled=true;
        }
        if (index >= 0 && index < $scope.yearArray.length - 1 && mode === 'minus') {
            this.year = $scope.yearArray[index - 1];
        } else{
            $scope.isMinusButtondisabled=true;
        }
    }

我正在通過傳遞加號減號模式在同一功能中執行增量和減量運算

這里有一些問題:

  1. 如果單擊加號按鈕,則禁用減號按鈕,反之亦然,因為您在每個if語句中檢查了mode
  2. 減號按鈕if語句需要檢查index是否> 0而不是>= 0
  3. 每當您遞減/遞增時,都應確保重新啟用另一個按鈕,以便可以向上/向下返回年份。

我建議是通過檢查啟動modeplus還是minus ,然后再從那里。

當它們plus年份時,然后檢查index並根據需要增加/禁用plus按鈕(然后重新啟用minus按鈕)。 而反之為minus模式。

    $scope.navigateYear=function(year, mode) {
       const index = $scope.yearArray.indexOf(year);

       if (mode === 'plus') {
         if (index >= 0 && index < $scope.yearArray.length - 1) {
               this.year = $scope.yearArray[index + 1];
               $scope.isMinusButtondisabled = false;
          }else{
            $scope.isPlusButtondisabled=true;
          }
       } else if (mode === 'minus') {
         if (index > 0 && index < $scope.yearArray.length) {
              this.year = $scope.yearArray[index - 1];
              $scope.isPlusButtondisabled = false;
          } else{
              $scope.isMinusButtondisabled=true;
          }
       }
    }

我希望這有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM