2017-07-28 62 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