Home > Web Front-end > JS Tutorial > How to Count Filtered Data in AngularJS's ng-repeat?

How to Count Filtered Data in AngularJS's ng-repeat?

Patricia Arquette
Release: 2024-11-11 15:53:03
Original
1078 people have browsed it

How to Count Filtered Data in AngularJS's ng-repeat?

Count Filtered ng-repeat Data

When utilizing AngularJS's ng-repeat directive to display data, it is essential to understand how to accurately count the number of filtered results.

Consider the following scenario:

var data = [
  {
    "name": "Jim",
    "age": 25
  },
  {
    "name": "Jerry",
    "age": 27
  }
];
Copy after login

To display the data filtered by a user query, the following code can be used:

<div ng-repeat="person in data | filter: query"></div>
Copy after login

However, the default count of displayed elements ({{data.length}}) will always reflect the total number of items in the original array, regardless of the filter applied. To obtain an accurate count of the filtered results, there are several options:

Angular 1.3

  • Use an alias expression:
<div ng-repeat="person in data | filter: query as filtered"></div>
Copy after login

Angular Prior to 1.3

  • Assign the filtered results to a new variable:
<div ng-repeat="person in filtered = (data | filter: query)"></div>
Copy after login

The filtered count can then be displayed as:

Showing {{filtered.length}} Persons
Copy after login

The above is the detailed content of How to Count Filtered Data in AngularJS's ng-repeat?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template