π§Ή AngularJS Pipes (Built-in & Custom Filters) β 2025 Guide
π§² Introduction β Why Learn AngularJS Pipes?
In AngularJS, what Angular (2+) calls “pipes”, AngularJS refers to as filters. These filters allow developers to format, transform, and manipulate data directly within templatesβall without needing to modify the actual model.
Filters are lightweight, reusable, and highly effective when displaying formatted data like currency, dates, or customized outputs in views.
π― In this guide, youβll learn:
- What filters (a.k.a. pipes) are in AngularJS
- How to use built-in filters like
currency,date,filter,limitTo,uppercase, etc. - How to create custom filters
- Best practices for clean formatting in templates
π§Ύ What Are Filters (Pipes) in AngularJS?
Filters in AngularJS are used to transform data before displaying it in the view. They can be applied inside {{ }} expressions or directives like ng-repeat.
β Syntax:
{{ expression | filterName[:parameter] }}
π§ Commonly Used Built-in Filters
| Filter | Description | Example Syntax |
|---|---|---|
currency | Formats a number as currency | `{{ price |
date | Formats date/time values | `{{ today |
uppercase | Converts text to uppercase | `{{ name |
lowercase | Converts text to lowercase | `{{ name |
filter | Filters an array based on a string or object | `ng-repeat=”user in users |
limitTo | Limits array/string output length | `{{ description |
orderBy | Sorts an array by a property | `ng-repeat=”item in items |
π‘ Built-in Filter Examples
β Currency Filter
<p>Price: {{ 1299.99 | currency:'βΉ' }}</p>
Output:Price: βΉ1,299.99
β Date Filter
<p>Today is: {{ today | date:'fullDate' }}</p>
$scope.today = new Date();
β Uppercase/Lowercase Filter
<p>{{ 'angularjs' | uppercase }}</p>
<!-- Output: ANGULARJS -->
β LimitTo Filter
<p>{{ longText | limitTo: 50 }}...</p>
Truncates text to 50 characters.
π Filter in ng-repeat
β Live Search Example
<input type="text" ng-model="searchText" placeholder="Search users">
<ul>
<li ng-repeat="user in users | filter:searchText">{{ user.name }}</li>
</ul>
$scope.users = [
{ name: 'Alice' },
{ name: 'Bob' },
{ name: 'Charlie' }
];
π§Ύ Typing in the input dynamically filters the user list.
π¨ Creating a Custom Filter
β Step-by-Step Example: Capitalize Filter
app.filter('capitalize', function() {
return function(input) {
if (!input) return '';
return input.charAt(0).toUpperCase() + input.slice(1);
};
});
β Usage:
<p>{{ 'angularjs' | capitalize }}</p>
<!-- Output: Angularjs -->
π§ Real-World Custom Filters Use Cases
| Filter Name | Purpose |
|---|---|
truncate | Shorten long paragraphs or strings |
yesNo | Display ‘Yes’ or ‘No’ based on boolean |
statusLabel | Convert status codes to readable text |
phoneFormat | Format phone numbers (e.g., (123) 456) |
βοΈ Best Practices
βοΈ Keep filters pureβdonβt modify external state
βοΈ Use filters only in templatesβnot in controllers
βοΈ Avoid heavy computation inside filters
βοΈ Chain multiple filters carefully to avoid performance issues
βοΈ Use limitTo and filter in ng-repeat for pagination/search
π Summary β Recap & Next Steps
AngularJS filters (a.k.a. pipes) are a simple yet powerful way to transform data presentation without touching your actual data model. With built-in and custom filters, you can format dates, numbers, strings, and lists effortlessly.
π Key Takeaways:
- Filters format or transform output before rendering
- Built-in filters like
currency,date,uppercase, andfilterare very handy - Custom filters extend formatting control with reusable logic
- Filters should be used only in templatesβnot business logic
βοΈ Real-world Relevance:
Perfect for dashboards, listings, live search, data formatting, and reusable display logic across your AngularJS app.
β FAQ β AngularJS Filters (Pipes)
β What are filters in AngularJS?
β
Filters are functions used to format or transform data before displaying it in the view. They work like pipes in Angular (2+).
β Can I use multiple filters together?
β
Yes. You can chain filters using the pipe symbol (|). Example:
{{ value | uppercase | limitTo:5 }}
β What is the difference between a filter and a directive?
β
A filter transforms output values, while a directive attaches behavior to DOM elements.
β Where should filters be used?
β
Use filters inside templates like {{ expression | filter }} or within ng-repeat. Avoid using them in controllers.
Share Now :
