Skip Nav

Learn Angular the right way

Our First Custom Filter

❶Sign up or log in Sign up using Google. Podcast Changing Careers to Coding January 22,

Creating a custom filter (pipe) in Angular

Anatomy of an Angular Filter
Table of contents
Angular 1.6 is here, this is what you need to know

Unfortunately, the built-in currency filter does not support this functionality, so we'll build our own that does! Angular exposes a simple API for creating a filter.

Just as you would declare a controller with app. A filter is very similar to an factory or service in many regards but has the added advantage of behaving on a global scope once created.

Let's break down the structure of a filter. This may seem confusing from the get go, so let's jump to some examples that will demystify writing custom filters. Let's start off slow and simple. The first custom filter we'll write will convert numbers to their ordinal values, meaning that if we apply our ordinal filter to say the number 43, what will be displayed is "43rd". Let's look at the code for our ordinal filter.

It is a good practice to ensure you have appropriate data to filter, and if you do not simply return the unmodified data back. Take a look at the CodePen below for some additional examples.

Sorry for the bad joke. The next custom filter we build will capitalize either the first letter or a letter we specify. The additional parameter will specify which letter to capitalize, if no additional parameter is passed than the first letter will be capitalized. This is a bit of a contrived example and has no real practical uses but we'll use it to show off how you could extend your filters. Again, applying this filter is very simple. If we wanted to capitalize a specific letter, we could pass the optional parameter such as:.

In the previous examples, we applied filters to single items, now let's apply a filter to a collection. In this example, we will actually filter a data set. In programming, there are hundreds of ways to reach the end goal, and in this example what we'll filter a list and return only the items that match a certain criteria. We will go through a list of programming languages and display only the statically typed ones.

This searchValue will then directly map across to the function argument anotherValue in the previous code example. This technique of filtering in a Controller is most favoured in Angular 1.

For Angular 2, we'll be using the same ordinal pipe and demonstrating how to create it. With Angular 2, we need a class and sprinkle some decorators, so let's get started! To get setup, we need to import Pipe and PipeTransform from the Angular 2 core:.

Next, we need to export and decorator our class with the right metadata and also use implements PipeTransform:. The next step is implementing a method named transform , of which is required to create custom Angular 2 pipes. In our case, we are expecting a number being passed in and a string as the return value:.

And that's the Angular 2 equivalent of creating a filter, so let's go implement it inside our component. To use our pipe, we can create a component, add our OrdinalPipe import to the NgModule inside the declarations Array, and we're good to go. We'll save NgModule and other fun stuff above for another guide.

Onto the function arguments in custom pipes! Just like with Angular 1. First, we need to tell the component that it has a provider:. Then we can use dependency injection to inject the OrdinalPipe into the constructor , which makes it available privately as this. Software development articles from the Telerik Developer Network and around the web. Click here to sample the latest issue.

Should you learn Angular 1.x or 2?

Main Topics

Privacy Policy

Building Custom AngularJS Filters. Ado Kukic (@kukicado) February 02, 0 Comments , Views so let's jump to some examples that will demystify writing custom filters. Today we built our own custom AngularJS filters. We learned how to create filters from scratch, built filters that did single tasks and created filters that had.

Privacy FAQs

Angular filters are one of the toughest concepts to work with. They’re a little misunderstood and it actually hurt my brain whilst learning them. Everything about custom filters in AngularJS Tagged in AngularJS • May 28, • 6 mins read • so we’re technically writing a function that gets passed into our.

About Our Ads

Creating custom filters Writing your own filter is very easy: just register a new filter factory function with your module. Internally, this uses the filterProvider. AngularJS is what HTML would have been, had it been designed for building web-apps. Declarative templates with data-binding, MVC, dependency injection and great testability story all implemented with pure client-side JavaScript!

Cookie Info

Angular provides the filter service which can be used to create our custom filter. The 'Demofilter' is a name given to our filter. This is the standard way in which a custom filter is . AngularJS custom search data by writing custom filter. Ask Question. There's an example of this with a Plunker in the angular docs for filter. AngularJS: searching data client side by custom filter. 0. Angular Operator Searching. 1. JavaScript Selecting Filter. Related.