---
title: "Custom Filters in AngularJS"  
description: "Custom filters in AngularJS provide a powerful way to format and transform data for display."  
author: "Ashutosh Patel"  
published: 2024-06-20  
updated: 2024-06-20  
canonical: https://www.mindstick.com/blog/304399/custom-filters-in-angularjs  
category: "angular js"  
tags: ["javascript", "angular js", "angularjs filter"]  
reading_time: 2 minutes  

---

# Custom Filters in AngularJS

#### Angular js Custom Filter

In AngularJS, **custom filters** are used to set the value of an [expression](https://www.mindstick.com/articles/1861/sqlite-expressions) to be displayed to the user. `{{ expression | filterName:argument }}` Syntax, where `filterName` is the name of the [optional](https://www.mindstick.com/interview/22851/main-difference-between-optional-and-required-keywords) filter function.

#### Defining Custom Filters

Here are the way to define the [custom filter](https://www.mindstick.com/interview/34020/how-do-filters-work-in-angularjs-and-can-you-create-a-custom-filter) in [Angular js](https://www.mindstick.com/forum/33800/how-to-add-edit-update-record-in-angular-js-in-mvc),

**Using the** `filter` **method**

```javascript
angular.module('myApp', [])
 .filter('customFilter', function() {
   return function(input, arg1, arg2) {
     // Filter logic here
     return /* transformed output */;
   };
 });
```

- `filter('customFilter', function() {...})` Defines a filter named customFilter.
- `return function(input, arg1, arg2) { ... }` The middle function is a filter function that accepts `input`(the value to be selected) and any other [arguments](https://answers.mindstick.com/qa/92535/what-are-the-different-types-of-arguments) (`arg1`, `arg2`).

**Injecting dependencies**\
If you need custom filter dependencies (e.g., other [functions](https://www.mindstick.com/forum/160140/explain-the-role-of-functions-as-a-service-faas-in-serverless-computing) or utilities), add them to the filter [function as](https://www.mindstick.com/forum/212/can-be-declare-a-static-function-as-virtual) you would for any AngularJS component.

#### Custom Filters

Once defined, you can apply **custom filters** to your HTML template.

```html
<div ng-controller="MyController">
 <p>{{ someText | customFilter:arg1:arg2 }}</p>
</div>
```

`someText` Text to change.\
`customFilter` The name of a predefined filter.\
`arg1`, `arg2` Optional text passed to the filter function.

## Example-

Here is a simple Angular js example to demonstrates the custom filter,

```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'>
    <!-- bootstrap cdn -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- angular js cdn -->
    <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">
      <h3>Text transform into lower case using custom filter</h3>
        <table class="table my-2">
            <thead>
              <tr>
                <th scope="col">#</th>
                <th scope="col">Name</th>
              </tr>
            </thead>
            <!-- bind the data from array with filter and orderBy -->
            <tbody data-ng-repeat="name in names | orderBy : name">
              <tr>
                <th scope="row">{{$index + 1}}</th>
                <!-- add custom filter here -->
                <td data-ng-bind="name | customFilter"></td>
              </tr>
            </tbody>
          </table>
    </div>

    <script>
        angular.module("myApp", [])
        .controller("myController", ['$scope', function($scope){
          // Array data
            $scope.names = [ "Jani", "Carl", "Margareth", "Hege", "Doe", "Gustav", "Birgit", "Mary", "Kai" ];
        }])
        // create custom filter that return each value in lower case
        .filter('customFilter', function(){
            return function(name){
                return name.toLowerCase();
            }
        });
    </script>
</body>
</html>
```

## In the example above-

- `$scope.names` is the array of data.
- `customFilter` is the name of the created custom filter.
- Converted text to lower-case using the [JavaScript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript) `toLowerCase(`) method.

## Output-

![Custom Filters in AngularJS](https://www.mindstick.com/blogs/dade54b9-c74b-4213-ba33-2917069bd88d/images/43885005-4ba9-4cca-9cd7-e3cbeda247aa.png)

**Also, Read:** [What is $rootScope in AngularJS?](https://www.mindstick.com/interview/33922/what-is-rootscope-in-angularjs)

---

Original Source: https://www.mindstick.com/blog/304399/custom-filters-in-angularjs

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
