简体   繁体   中英

AngularJS ng-show icons not working properly after sorting

I'm implementing sorting for an ng-repeat and the sorting itself works fine.

However, when it comes to showing/hiding the carets on each button, only the first button's caret switches from up to down, regardless of which button I click.

Code below: 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>

Controller:

$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;
};

you don't need two functions. you can do it by using single function with using ng-show and ng-hide directive. like this below way. But am didn't check when the showAndHidCaretIcon() function is triggering.

<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>

and

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM