---
title: "How to use a filter in a controller?"  
description: "How to use a filter in a controller?"  
author: "Revati S Misra"  
published: 2024-06-17  
updated: 2024-06-18  
canonical: https://www.mindstick.com/forum/160750/how-to-use-a-filter-in-a-controller  
category: "angular js"  
tags: ["javascript", "angular js", "angularjs filter"]  
reading_time: 3 minutes  

---

# How to use a filter in a controller?

How to use a [filter](https://www.mindstick.com/forum/1176/filter-in-a-xml-file-in-c-sharp) in a [controller](https://www.mindstick.com/blog/273/passing-values-from-controller-to-view-in-asp-dot-net-mvc)?

## Replies

### Reply by Ashutosh Patel

### Filter in AngularJS

Using filters in AngularJS is a powerful way to format and change data displayed in views. Filters can be applied directly in templates to format the data without actually changing the underlying data.

AngularJS provides some built-in filters that you can use easily in your program,

- **Filter-** Filters an array based on a predicate function or string.

```html
<table>
    <thead>
        <tr>
            <th>Name</th>
        </tr>
    </thead>
    <tbody data-ng-repeat="item in items | filter: searchText">
        <tr>
            <!-- bind the filtered data -->
            <td data-ng-bind="item.name"></td>
        </tr>
    </tbody>
</table>
```

- **Order By-** Orders an array based on an expression.

```html
<table>
    <thead>
        <tr>
            <th>Name</th>
        </tr>
    </thead>
    <tbody data-ng-repeat="item in items | orderBy : propertyName">
        <tr>
            <!-- bind the ordered data -->
            <td data-ng-bind="item.name"></td>
        </tr>
    </tbody>
</table>
```

## Example-

Here is an example of how to use filters in angular js program

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

<body>
    <div data-ng-controller="MyController" class="container my-4">
        <!-- input field for filter data  -->
        <input type="text" data-ng-model="name" class="form-control col-4" placeholder="type to filter"/>
        <div>
            <h2 data-ng-hide="!name" class="my-2">Filtered data on "{{name}}"</h2>
            <table class="table my-2">
                <thead>
                    <tr>
                        <!-- the OrderBy() function for ordering data based on column-->
                        <th data-ng-click="OrderBy('$index')">#</th>
                        <th  data-ng-click="OrderBy('name')">Name</th>
                        <th  data-ng-click="OrderBy('country')">Country</th>
                    </tr>
                </thead>
                <!-- bind data using loop in table row -->
                 <!-- apply the built-in service filter with column name  -->
                  <!-- also apply the ordering based on column data when click on column name -->
                <tbody data-ng-repeat="name in names | filter: name | orderBy : myOrder">
                    <tr>
                        <!-- angular service $index use for serial number -->
                        <td data-ng-bind="$index+1"></td>
                        <td data-ng-bind="name.name"></td>
                        <td data-ng-bind="name.country"></td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>

    <script>
        angular.module("myApp", [])
        // json data for apply filter
        .controller("MyController", 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 that orders the data dynamically
            $scope.OrderBy = function(data){
                $scope.myOrder = data;
            }
        });

    </script>

</body>
</html>
```

## In the example above

- A dummy json data taken in the `$scope.names`
- bind the data into table in HTML template using `ng-repead`
- apply the `filter`and `orderBy`filters during fetch data from **json** object and bind into HTML **table body**
- `$scope.OrderBy` function respponsible for oders the data when click on column name in the HTML table on the View.

## Output-

![How to use a filter in a controller?](https://www.mindstick.com/mindstickforums/3b66f3a0-f749-42ad-9b6c-127e92d956ca/images/6c19f3c6-f979-4bcb-82d6-b3aaa64816fe.png)

By using filters in AngularJS templates you can keep your visual logic clean and readable while providing powerful data transformation capabilities.

**Also, Read:** [If else statement in AngularJS.](https://www.mindstick.com/forum/160749/if-else-statement-in-angularjs)


---

Original Source: https://www.mindstick.com/forum/160750/how-to-use-a-filter-in-a-controller

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
