In AngularJS, filters are used to format or manipulate data in the view. They can be applied to expressions in the HTML to transform the data before it's displayed. Filters are often used for tasks such as formatting dates, sorting arrays, converting text to uppercase or lowercase, and more.
Here's how you can use filters in AngularJS:
Basic Syntax:
{{ expression | filter:argument1:argument2:... }}
expression: The data you want to filter.
filter: The name of the filter to apply.
arguments: Optional arguments that can be passed to the filter.
Examples:
Uppercase Filter:
<!-- Converts text to uppercase -->
<p>{{ 'hello' | uppercase }}</p> <!-- Output: HELLO -->
Lowercase Filter:
<!-- Converts text to lowercase -->
<p>{{ 'WORLD' | lowercase }}</p> <!-- Output: world -->
Number Filter:
<!-- Formats a number with specified decimal places -->
<p>{{ 12345.6789 | number:2 }}</p> <!-- Output: 12,345.68 -->
Date Filter:
<!-- Formats a date -->
<p>{{ today | date:'yyyy-MM-dd HH:mm:ss' }}</p>
In the controller:
// Assuming 'today' is a Date object
$scope.today = new Date();
Filtering Arrays:
<!-- Filters an array based on a specified criteria -->
<ul>
<li ng-repeat="item in items | filter:'searchText'">{{ item.name }}</li>
</ul>
You can also create custom filters by registering a new filter function using the
filter method in your AngularJS module.
// Example of a custom filter named 'reverse'
angular.module('myApp').filter('reverse', function() {
return function(input) {
return input.split('').reverse().join('');
};
});
<!-- Using the custom 'reverse' filter -->
<p>{{ 'hello' | reverse }}</p> <!-- Output: olleh -->
Chaining Filters:
Filters can be chained to perform multiple transformations on the data.
<p>{{ price | currency | uppercase }}</p>
In this example, the currency filter formats the price, and then the
uppercase filter converts it to uppercase.
Common Use Cases:
Formatting Numbers and Currencies:
Use number and currency filters to format numeric values.
Date Formatting:
The date filter is useful for formatting date objects into human-readable strings.
Text Transformations:
uppercase and lowercase filters can be used to change the case of text.
Filtering and Sorting Lists:
The filter and orderBy filters are helpful for filtering and sorting arrays or lists.
Custom Transformations:
Create custom filters for specific data transformations that are not covered by built-in filters.
By leveraging filters in AngularJS, you can easily manipulate and format data in a clean and declarative manner, enhancing the presentation of your application's user interface.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In AngularJS, filters are used to format or manipulate data in the view. They can be applied to expressions in the HTML to transform the data before it's displayed. Filters are often used for tasks such as formatting dates, sorting arrays, converting text to uppercase or lowercase, and more.
Here's how you can use filters in AngularJS:
Basic Syntax:
Examples:
In the controller:
Custom Filters:
You can also create custom filters by registering a new filter function using the filter method in your AngularJS module.
Chaining Filters:
Filters can be chained to perform multiple transformations on the data.
In this example, the currency filter formats the price, and then the uppercase filter converts it to uppercase.
Common Use Cases:
Formatting Numbers and Currencies:
Date Formatting:
Text Transformations:
Filtering and Sorting Lists:
Custom Transformations:
By leveraging filters in AngularJS, you can easily manipulate and format data in a clean and declarative manner, enhancing the presentation of your application's user interface.