Ng-Repeat Filter in Angular

  1. Understanding ng-repeat
  2. Using Filters with ng-repeat
  3. Advanced Filtering Techniques
  4. Conclusion
  5. FAQ
Ng-Repeat Filter in Angular

In the world of web development, Angular has emerged as a powerful framework for building dynamic applications. One of its standout features is the ng-repeat directive, which allows developers to efficiently render lists of data. But did you know that you can enhance ng-repeat with filters?

This tutorial will guide you through the process of using filters in Angular’s ng-repeat, enabling you to display data more effectively and improve user experience. Whether you want to filter by search terms or by specific criteria, mastering the ng-repeat filter will elevate your Angular skills. Let’s dive in!

Understanding ng-repeat

At its core, ng-repeat is an Angular directive that allows you to loop through an array or object and render its contents in the DOM. This is particularly useful when displaying lists, such as user profiles, product catalogs, or any other data that can be represented in a tabular format.

To use ng-repeat, you simply define the directive in your HTML, specifying the array you want to iterate over. Here’s a basic example:

HTML
 htmlCopy<div ng-repeat="item in items">
    {{ item.name }}
</div>

This code snippet will render the name of each item in the items array. However, this is just the beginning. By integrating filters, you can refine what gets displayed based on user input or other conditions.

Using Filters with ng-repeat

Filters in Angular allow you to transform the data displayed by ng-repeat. They can be used to sort, limit, or even filter the data based on specific criteria. The beauty of Angular filters is their flexibility and ease of use.

Here’s how you can implement a simple filter with ng-repeat:

Example: Filtering a List of Items

Assume you have an array of items and you want to filter them based on a search input. Below is a practical example:

HTML
 htmlCopy<input type="text" ng-model="searchText" placeholder="Search items...">

<ul>
    <li ng-repeat="item in items | filter:searchText">
        {{ item.name }}
    </li>
</ul>

In this example, the ng-model directive binds the input field to a variable called searchText. The filter filter is applied to ng-repeat, allowing the list to dynamically update based on the user’s input.

Output:

 textCopy- Item 1
- Item 2
- Item 3

When the user types in the search box, the list will automatically filter to show only those items that match the input text. This is a powerful feature that enhances user interaction by providing instant feedback.

Advanced Filtering Techniques

While basic filtering is straightforward, you may often need to implement more complex filtering logic. For instance, you might want to filter based on multiple criteria or sort the results. Here’s how you can achieve that:

Example: Filtering by Multiple Criteria

Let’s say you want to filter items based on both a category and a search term. You can create a more complex filter function in your Angular controller.

JavaScript
 javascriptCopy$scope.filterItems = function(item) {
    return (!$scope.searchText || item.name.includes($scope.searchText)) &&
           (!$scope.selectedCategory || item.category === $scope.selectedCategory);
};

In your HTML, you can then use this function with ng-repeat:

HTML
 htmlCopy<select ng-model="selectedCategory">
    <option value="">All Categories</option>
    <option value="Category1">Category 1</option>
    <option value="Category2">Category 2</option>
</select>

<ul>
    <li ng-repeat="item in items | filter:filterItems">
        {{ item.name }}
    </li>
</ul>

Output:

 textCopy- Filtered Item 1
- Filtered Item 2

Here, the filterItems function combines both the search text and the selected category to filter the displayed items. This approach provides a more tailored experience for users, allowing them to find exactly what they need.

Conclusion

Using filters with ng-repeat in Angular can significantly enhance the way you display data in your applications. By allowing users to search and filter through lists, you improve usability and overall user experience. Whether you are working on a simple project or a complex application, mastering ng-repeat and its filtering capabilities is an essential skill for any Angular developer. With the examples and techniques outlined in this tutorial, you now have the tools to implement effective filtering in your own projects. Happy coding!

FAQ

  1. What is ng-repeat in Angular?
    ng-repeat is a directive in Angular that allows you to iterate over an array or object and display its contents in the DOM.

  2. How do I filter items with ng-repeat?
    You can filter items by using the filter filter in your ng-repeat expression, combined with a model bound to an input field.

  3. Can I filter by multiple criteria in Angular?
    Yes, you can create a custom filter function in your controller to filter items based on multiple criteria.

  4. What are some common use cases for ng-repeat filters?
    Common use cases include searching through lists, sorting items, and filtering based on user selections.

  5. Is it possible to sort items while using ng-repeat?
    Yes, you can use the orderBy filter in conjunction with ng-repeat to sort items based on specific properties.

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
Rana Hasnain Khan avatar Rana Hasnain Khan avatar

Rana is a computer science graduate passionate about helping people to build and diagnose scalable web application problems and problems developers face across the full-stack.

LinkedIn