AngularJS Filters Example Tutorial

We looked at View Model, View and Controller concepts in the previous post. Now we are going to discuss about the AngularJS Filters.

Contents

AngularJS Filter Concepts

Filters are used to format the value of an expression for display to the user. They can be added to templates, controllers, directives or services and you can also define your own filters. Using filters, Angular lets you organize data, so that it only displays data if it meets certain criteria.

We use pipe ( | ) character to add angular filters. We will see the use of filters in the Angular Filters in Action section.

The following table briefly describes the commonly used Angular Filters.

Angular Filter Description
filter Selects a subset of items from an array and returns it as a new array.
currency Formats a number to currency format
number Used to format a number.
date Formats date to a string based on the requested format.
json convert JavaScript object to JSON
lowercase Converts a string to lowercase.
uppercase converts a string to uppercase
limitTo Creates a new array or string containing only a specified number of elements.
orderBy Orders an array by the expression.

AngularJS Filters in Action

We will see the use of Angular filters in this section.

Uppercase and Lowercase

The following example demonstrates the use of uppercase and lowercase filters in AngularJS.

filter-case.html

The following Controller is used in this example. We set the initial state of the scope by attaching the properties with upper and lowercase letters.

blogController.js

Currency

The following example demonstrates the use of currency filter in AngularJS.

filter-currency.html

The following Controller is used in this example. We set the initial state of the scope by attaching the properties quantity and price with values 1 and 10 respectively.

currencyController.js

You will see the following output in your browser. The default currency format is in USD($).

Using orderby and filter

The following example demonstrates the use of angular filters like orderby and filter. We use orderby filter to order the players array by the given expression. In this example, we use name to order the players array. It is ordered alphabetically for strings and numerically for numbers.
We use filter to select a subset of items from players array of objects and returns a new array which matches the filter expression. In this example you can see how filter works as you type.

filter-orderby.html

We use the following controller in the above example.

playerController.js

You will see the following output in your browser. You can type in the input field to filter the list.

Using limitTo

We use limitTo filter to create a new array or string containing only a specified number of elements. The following example demonstrates the use of limitTo filter

filter-limit.html

We use the following controller in our example.

limitController.js

You will see the following output in your browser.

You can try using the Angular Filters alone or in combinations depending on your requirements. That’s all for AngularJS filters and you will see more angular features in the coming posts.

By admin

Leave a Reply

%d bloggers like this: