2017-07-28 49 views
1

我正在執行ng-repeat的排序並且排序本身正常工作。AngularJS ng-show圖標在排序後無法正常工作

但是,當涉及在每個按鈕上顯示/隱藏插入符時,無論點擊哪個按鈕,只有第一個按鈕的插入符會從上至下切換。下面

代碼: HTML:

<button ng-click="sortQuestions('createdAt')" class="sort-button chip grey darken-2 white-text">Newest 
     <span ng-show="showCaretDown('createdAt') === true"><i class="fa fa-caret-down"></i></span> 
     <span ng-show="showCaretUp('createdAt') === true"><i class="fa fa-caret-up"></i></span> 
    </button> 
    <button ng-click="sortQuestions('updatedAt')" class="sort-button chip grey darken-2 white-text">Last Updated 
     <span ng-show="showCaretDown('updatedAt') === true"><i class="fa fa-caret-down"></i></span> 
     <span ng-show="showCaretUp('updatedAt') === true"><i class="fa fa-caret-up"></i></span> 
    </button> 
    <button ng-click="sortQuestions('numberOfAnswers')" class="sort-button chip grey darken-2 white-text">Answers 
     <span ng-show="showCaretDown('numberOfAnswers') === true"><i class="fa fa-caret-down"></i></span> 
     <span ng-show="showCaretUp('numberOfAnswers') === true"><i class="fa fa-caret-up"></i></span> 
    </button> 
    <button ng-disabled="true" ng-click="sortQuestions('votes')" class="sort-button chip grey darken-2 white-text">Votess 
     <span ng-show="showCaretDown('votes')"><i class="fa fa-caret-down"></i></span> 
     <span ng-show="showCaretUp('votes')"><i class="fa fa-caret-up"></i></span> 
    </button> 

    <div ng-repeat="question in questions | orderBy:sort.propertyName:sort.ascending" class="card-panel"> 
    .... 
    </div> 

控制器:

$scope.questions = []; 
$scope.sortAscending = true; 
$scope.sort = { 
    propertyName: 'createdAt', 
    ascending: true 
}; 


$scope.questions = questionService.getQuestions().then(function success(response) { 
    logger.info("Returned questions data: ", response.data); 
    $scope.questions = response.data._embedded.questions; 
    logger.info("$scope.questions: ", $scope.questions); 
    logger.info("Is $scope,questions an array? ", angular.isArray($scope.questions)); 
}, function error(response) { 
    logger.error("Error getting questions: ", response.data); 
    $scope.error = response.data; 
}); 

$scope.sortQuestions = function(sortPropertyName) { 
    logger.info("Sorting by ", sortPropertyName); 
    $scope.sort.properyName = sortPropertyName; 
    $scope.sort.ascending = !$scope.sort.ascending; 
}; 

$scope.showCaretDown = function(sortPropertyName) { 
    return $scope.sort.propertyName === sortPropertyName && !$scope.sort.ascending; 
}; 

$scope.showCaretUp = function(sortPropertyName) { 
    return $scope.sort.propertyName === sortPropertyName && $scope.sort.ascending; 
}; 

回答

0

你不需要兩個功能。您可以通過使用ng-showng-hide指令使用單一功能來實現。像下面這樣。但是我沒有檢查什麼時候觸發了showAndHidCaretIcon()函數。

<button ng-click="sortQuestions('createdAt')" class="sort-button chip grey darken-2 white-text">Newest 
     <span ng-show="showAndHidCaretIcon('createdAt') === true"><i class="fa fa-caret-down"></i></span> 
     <span ng-hide="showAndHidCaretIcon('createdAt') === true"><i class="fa fa-caret-up"></i></span> 
    </button> 

$scope.showAndHidCaretIcon = function(sortPropertyName) { 
    return ($scope.sort.propertyName === sortPropertyName && $scope.sort.ascending); 
}; 
+0

添加到了第2個按鈕 - 它給了我同樣的結果(即,如果該按鈕我點擊,只有第一個按鈕的變化,無論),除了有現在總是是一個向下在其他按鈕上的箭頭。 – MC123

+0

並感謝您的建議移動到一個功能:) – MC123