Pipes (also known as filters in AngularJS) are a powerful feature that allows you to transform data displayed in your templates. AngularJS provides several built-in filters, but there are times when you need to create custom filters to meet your specific needs. This article will guide you through the process of creating custom pipes (filters) in AngularJS.
Prerequisites
- Basic understanding of AngularJS.
- AngularJS development environment set up.
What is a Pipe/Filter?
In AngularJS, filters allow you to format the data displayed to the user. They can be used in view templates, controllers, or services. Filters are easy to use and come with built-in support for chaining, which means you can use multiple filters together to achieve the desired output.
Built-in Filters
AngularJS provides several built-in filters, such as.
- currency: Formats a number as a currency.
- date: Format date to a specified format.
- filter: Selects a subset of items from an array.
- json: Formats an object as a JSON string.
- lowercase: Converts a string to lowercase.
- uppercase: Converts a string to uppercase.
- number: Formats a number as text.
- orderBy: Orders an array by an expression.
- limitTo: Creates a new array or string containing only a specified number of elements.
Creating a Custom Pipe/Filter
To create a custom filter, you need to register it with your AngularJS module using the filter method. A filter is simply a function that takes input, processes it, and returns the processed output.
Below is an AngularJS application that demonstrates the use of several built-in filters (currency, date, filter, json, lowercase, uppercase, number, orderBy, limitTo) along with the custom capitalize filter we created earlier.
HTML File (index.html)
<!DOCTYPE html>
<html ng-app="customFilterApp">
<head>
<title>AngularJS Filters Example</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainController">
<h1>AngularJS Filters Example</h1>
<!-- Currency Filter -->
<h2>Currency Filter:</h2>
<p>{{ amount | currency }}</p>
<!-- Date Filter -->
<h2>Date Filter:</h2>
<p>{{ currentDate | date:'fullDate' }}</p>
<!-- Filter Filter -->
<h2>Filter Filter:</h2>
<p>Original Array: {{ names | json }}</p>
<p>Filtered Array: {{ names | filter:'John' }}</p>
<!-- JSON Filter -->
<h2>JSON Filter:</h2>
<p>{{ person | json }}</p>
<!-- Lowercase Filter -->
<h2>Lowercase Filter:</h2>
<p>{{ message | lowercase }}</p>
<!-- Uppercase Filter -->
<h2>Uppercase Filter:</h2>
<p>{{ message | uppercase }}</p>
<!-- Number Filter -->
<h2>Number Filter:</h2>
<p>{{ number | number:2 }}</p>
<!-- OrderBy Filter -->
<h2>OrderBy Filter:</h2>
<p>Original Array: {{ people | json }}</p>
<p>Ordered Array by Name: {{ people | orderBy:'name' | json }}</p>
<!-- LimitTo Filter -->
<h2>LimitTo Filter:</h2>
<p>{{ names | limitTo:2 | json }}</p>
<!-- Custom Capitalize Filter -->
<h2>Custom Capitalize Filter:</h2>
<p>{{ message | capitalize }}</p>
</body>
</html>

Join the conversation! Your thoughts help the community grow.