How do filters work in AngularJS?
How do filters work in AngularJS?
129
19-Jun-2024
Updated on 19-Jun-2024
Ashutosh Kumar Verma
19-Jun-2024AngularJS Filters
Filters in AngularJS are used to set the value of an expression to be displayed to the user. They can be used in visual templates programmatically in the main syntax system ({{ word | filter:options }}) or in controllers and services.
Also, Read: How to display length of filtered ng-repeat data?
How Filters works
Here are few points that explains the working of filters in Angular application,
Usage in Templates-
Filters are applied to expressions in double curly braces (
{{ }}
) or directives such as ng-bind. Syntax{{ expression | Filter:Options }}
Example<p>{{ dateValue | date:'yyyy-MM-dd' }}</p>
This example has the worddateValue
, the filter name date, and the filter parameters (options) 'yyyy-MM-dd
'.Chaining Filters
Filters can be chained using the pipe (
|
) symbol. Example<p>{{ textValue | uppercase | limitTo:20 }}</p>
Here, thetextValue
is first converted to uppercase (uppercase
filter), then limited to 20 characters (limitTo
filter).Filtering Arrays Filters can also be used to filter an array based on specific criteria using
filter
.Example-
In this example, objects are lists of
items
, andsearchText
is a pattern bound to the input field. AngularJS will filter objects based on thesearchText
value.Example-
Output-
Filters in AngularJS provide a powerful way to organize and manipulate data directly in templates or programmatically. They contribute to separation of concerns by allowing data transformation logic to be cached and reused in different parts of the application.
Also, Read: How orderBy multiple fields in Angular?