---
title: "How do filters work in AngularJS?"  
description: "How do filters work in AngularJS?"  
author: "Steilla Mitchel"  
published: 2024-06-19  
updated: 2024-06-19  
canonical: https://www.mindstick.com/forum/160765/how-do-filters-work-in-angularjs  
category: "angular js"  
tags: ["javascript", "angular js", "angularjs filter"]  
reading_time: 3 minutes  

---

# How do filters work in AngularJS?

How do [filters work in AngularJS](https://www.mindstick.com/interview/34020/how-do-filters-work-in-angularjs-and-can-you-create-a-custom-filter)?

## Replies

### Reply by Ashutosh Patel

#### AngularJS Filters

[Filters](https://www.mindstick.com/forum/156119/how-to-use-filters-in-angularjs) in [AngularJS](https://www.mindstick.com/forum/33926/use-angularjs-without-scope-in-mvc) are used to set the value of an expression to be displayed to the user. They can be used in visual templates programmatically in the main syntax system ({{ word | filter:options }}) or in controllers and services.

**Also, Read:** [How to display length of filtered ng-repeat data?](https://www.mindstick.com/forum/160752/how-to-display-length-of-filtered-ng-repeat-data)

#### How Filters works

Here are few points that explains the working of filters in Angular application,

## Usage in Templates-

Filters are applied to expressions in double curly braces (`{{ }}`) or directives such as ng-bind. **Syntax**

`{{ expression | Filter:Options }}` **Example**

`<p>{{ dateValue | date:'yyyy-MM-dd' }}</p>` This example has the word `dateValue`, the filter name date, and the filter parameters (options) '`yyyy-MM-dd`'.

## Chaining Filters

Filters can be chained using the pipe (`|`) symbol. **Example**

`<p>{{ textValue | uppercase | limitTo:20 }}</p>` Here, the `textValue` is first converted to uppercase (`uppercase` filter), then limited to 20 characters (`limitTo` filter).

**Filtering Arrays** Filters can also be used to **filter** an array based on specific criteria using `filter`.

## Example-

```html
<div ng-repeat="item in items | filter:searchText">
	 {{ item.name }}
</div>
```

In this example, objects are lists of `items`, and `searchText`is a pattern bound to the input field. AngularJS will filter objects based on the `searchText`value.

## Example-

```html
<!DOCTYPE html>
<html data-ng-app="myApp">
<head>
    <meta charset='utf-8'>
    <meta http-equiv='X-UA-Compatible' content='IE=edge'>
    <title>ng-Root Scope</title>
    <meta name='viewport' content='width=device-width, initial-scale=1'>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
</head>

<body data-ng-controller="myController">
    <div class="container my-4">

        <!-- type to filter data -->
        <input data-ng-model="name" class="form-control" placeholder="Type to filter" />
        <!-- show when input data in text field -->
        <h4 data-ng-hide="!name" class="my-2">filtered data on "{{name}}"</h4>
        <table class="table my-2">
            <thead>
              <tr>
                <th scope="col">#</th>
                <!-- click on headers apply order by name and country -->
                <th data-ng-click="Order('name')" >Name</th>
                <th data-ng-click="Order('country')" scope="col">Country</th>
              </tr>
            </thead>
            <!-- bind the data from array with filter and orderBy -->
            <tbody data-ng-repeat="data in names | filter: name | orderBy : myOrder ">
              <tr>
                <th scope="row">{{$index + 1}}</th>
                <td data-ng-bind="data.name"></td>
                <td data-ng-bind="data.country"></td>
              </tr>
            </tbody>
          </table>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"
    integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
    crossorigin="anonymous"></script>

    <script>
        angular.module("myApp", [])
        .controller("myController", ['$scope', function($scope){
            $scope.names = [
            {name:'Jani',country:'Norway'},
            {name:'Carl',country:'Sweden'},
            {name:'Margareth',country:'England'},
            {name:'Hege',country:'Norway'},
            {name:'Joe',country:'Denmark'},
            {name:'Gustav',country:'Sweden'},
            {name:'Birgit',country:'Denmark'},
            {name:'Mary',country:'England'},
            {name:'Kai',country:'Norway'}
            ];
            // function to apply order by
            $scope.Order = function(text)
            {
                $scope.myOrder = text;
            }
        }]);
    </script>

</body>
</html>
```

## Output-

![How do filters work in AngularJS?](https://www.mindstick.com/MindstickForums/53c861af-7e60-4736-bae1-ab6e2eccd4af/images/e315af74-9dcf-40c4-a1b4-c0b86f75d93b.png)

**Filters** in AngularJS provide a powerful way to organize and manipulate data directly in templates or programmatically. They contribute to separation of concerns by allowing data transformation logic to be cached and reused in different parts of the application.

**Also, Read:** [How orderBy multiple fields in Angular?](https://www.mindstick.com/forum/160754/how-orderby-multiple-fields-in-angular)


---

Original Source: https://www.mindstick.com/forum/160765/how-do-filters-work-in-angularjs

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
