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

后端 未结 3 1027
后悔当初
后悔当初 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条回答
  •  梦毁少年i
    2021-01-04 09:10

    Your issue is that ng-repeat is scope isolated. As a result you can't refer to the internal list that is being managed by ng-repeat from your controller/directive.

    As a result there are 2 options

    1. Bind the filtered list to ng-repeat from your controller/directive, so you maintain the filtered list.

      //in your controller
      $scope.filteredItems = $filter('yourFilter')($scope.items,$scope.searchText);
      $scope.$watch('searchText', function(newValue){
         $scope.filteredItems = $filter('yourFilter')($scope.items, newValue);
      });
      
      //in your view
      
          {{item.id}}
          {{item.name}}
      
      
    2. Perform the filter again in your controller/directive

      $scope.toggleAll() {
          var items = $filter('yourFilter')($scope.items, $scope.searchText);
          for(var i in items){
             //set your selected property
          }
      }
      

提交回复
热议问题