gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 04:21:42 25 4
gpt4 key购买 nike

我正在为 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>

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

最佳答案

你不需要两个函数。您可以通过使用 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);
};

关于javascript - AngularJS ng-show 图标在排序后无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45373471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com