AngularJS: Count Filtered Items

前端 未结 4 1727
醉话见心
醉话见心 2020-12-31 03:31

I am showing subsets of a list if a checkbox is checked. I would like to replace the X next to the checkbox with the count of the list matching the selection criteria. I hav

4条回答
  •  情话喂你
    2020-12-31 04:13

    Possible solution 1: Inline

    You could actually save a reference to the filtered results in a variable: h in filtered.marvel = (heroes | filter:{comic:'Marvel'}), which you could use like so: filtered.marvel.length.

    See: Plunkr

    Possible solution 2: In the controller

    You could also move this code to your controller:

    $scope.filteredHeroes.marvel = $filter('filter')($scope.heroes, {comic:'Marvel'});

    , which you could use by ng-repeat="hero in filteredHeroes.marvel"

    and {{filteredHeroes.marvel.length}}

    (Don't forget to add $filter as a controller dependency)

    See: Plunkr

提交回复
热议问题