排序后AngularJS ng-show图标无法正常工作

时间:2017-07-28 12:24:56

标签: javascript angularjs html5 controller ng-show

我正在实施ng-repeat的排序,排序本身也能正常工作。

但是,当显示/隐藏每个按钮上的插入符号时,无论我点击哪个按钮,只有第一个按钮的插入符号从上到下切换。

以下代码: 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;
};

1 个答案:

答案 0 :(得分: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);
};