[英]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;
}
}
我正在通過傳遞加號或減號模式在同一功能中執行增量和減量運算
這里有一些問題:
if
語句中檢查了mode
。 if
語句需要檢查index
是否> 0
而不是>= 0
我建議是通過檢查啟動mode
是plus
還是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.