🧠 AngularJS Directives & Pipes
Estimated reading: 3 minutes 37 views

🧹 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

FilterDescriptionExample Syntax
currencyFormats a number as currency`{{ price
dateFormats date/time values`{{ today
uppercaseConverts text to uppercase`{{ name
lowercaseConverts text to lowercase`{{ name
filterFilters an array based on a string or object`ng-repeat=”user in users
limitToLimits array/string output length`{{ description
orderBySorts 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 NamePurpose
truncateShorten long paragraphs or strings
yesNoDisplay ‘Yes’ or ‘No’ based on boolean
statusLabelConvert status codes to readable text
phoneFormatFormat 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, and filter are 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

🧹 AngularJS Pipes (Built-in & Custom)

Or Copy Link

CONTENTS
Scroll to Top