How to display length of filtered ng-repeat data

前端 未结 8 1350
花落未央
花落未央 2020-11-22 08:11

I have a data array which contains many objects (JSON format). The following can be assumed as the contents of this array:

var data = [
  {
    \"name\": \"         


        
8条回答
  •  天涯浪人
    2020-11-22 08:39

    ngRepeat creates a copy of the array when it applies a filter, so you can't use the source array to reference only the filtered elements.

    In your case, in may be better to apply the filter inside of your controller using the $filter service:

    function MainCtrl( $scope, filterFilter ) {
      // ...
    
      $scope.filteredData = myNormalData;
    
      $scope.$watch( 'myInputModel', function ( val ) {
        $scope.filteredData = filterFilter( myNormalData, val );
      });
    
      // ...
    }
    

    And then you use the filteredData property in your view instead. Here is a working Plunker: http://plnkr.co/edit/7c1l24rPkuKPOS5o2qtx?p=preview

提交回复
热议问题