How do filters work in AngularJS?
How do filters work in AngularJS?
I completed my post-graduation in 2013 in the engineering field. Engineering is the application of science and math to solve problems. Engineers figure out how things work and find practical uses for scientific discoveries. Scientists and inventors often get the credit for innovations that advance the human condition, but it is engineers who are instrumental in making those innovations available to the world. I love pet animals such as dogs, cats, etc.
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, thetextValueis first converted to uppercase (uppercasefilter), then limited to 20 characters (limitTofilter).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, andsearchTextis a pattern bound to the input field. AngularJS will filter objects based on thesearchTextvalue.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?