javascript - AngularJS ng-show icons not working properly after sorting -
i'm implementing sorting ng-repeat , sorting works fine.
however, when comes showing/hiding carets on each button, first button's caret switches down, regardless of button 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 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 ", 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 2 functions. can using single function using ng-show
, ng-hide
directive. below way. didn't check when showandhidcareticon()
function 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); };
Comments
Post a Comment