Using ng-repeat and filter, how to tell which items are visible?

后端 未结 3 1012
后悔当初
后悔当初 2021-01-04 08:36

I have an array of objects that I\'m displaying in my Angular app using ng-repeat. I\'m filtering out items using filter and the value of a search

3条回答
  •  北荒
    北荒 (楼主)
    2021-01-04 09:29

    Angular filters create a new array. So in order to perform an action on the filtered items you're going to have to capture the new array.

    Something like:

    $scope.toggleAll() {
      var filteredItems = $filter('filter')($scope.items, $scope.searchValue);
      for(var i in filteredItems) {
        ...
      }
    }
    

    If you don't want to filter twice you'll have to filter the array yourself every time searchValue changes and ng-repeat over that filtered array, ng-change is useful in that case.

提交回复
热议问题