When we want to develop an application, we always start the application with a simple basic common task; i.e., retrieve data, transform data and then display the data in front of the user through our user interface. Retrieving data from any type of data source totally depends on data service providers like web services, web APIs etc. So once data has arrived, we can push those raw data values directly to our user interface for viewing to the user. But sometimes, this does not happen exactly in the same manner. For example, in most use cases, users prefer to see a date in a simple format like 15/02/2017 rather than the raw string format Wed Feb 15, 2017, 00:00:00 GMT-0700 (Pacific Daylight Time). So it is clear from the above example, that some value requires editing before getting viewed into the user interface. Also, it can be noticed that the same type of transformation is required by us in many different user interfaces or vice versa. So, in this scenario, we need to think about some style type property which we can be created centrally and applied whenever we require it globally. So for this purpose, Angular 4 introduces Angular Pipes, a definite way to write display – value transformations that we can declare in our HTML.
What are Pipes?
In Angular 4.0, Pipes are classes implementing a single function interface, accepting an input value with an optional parameter array, returning a transformed value.
Transition From Filter to Pipes
In Angular 1.x, we are familiar with the term filter. Filters are a great way of returning a new collection of data or formatting the new. Filters are basically just a function, which take a single value or collection of values as input and returns a new value or collection of values based on the logical responsibilities. In Angular 4.0, pipes are the modernized version of filters. Most of the inbuilt filters of Angular 1.x have been converted as pipes in Angular 4.0 with some new pipes. Pipes are accessed in our templates in the same way that filters were--with the "pipe" character |. Below table shows the comparison of pipes or filters in both Angular 1.x and Angular 4.0.
In Angular 1.x version, filters are very helpful for formatting any type of value as output in our templates. So with Angular 4.0, we get the same feature, but now it is known as Pipes. Below table shows the comparison of pipes or filters in both Angular 1.x and Angular 4.0.
Filter / Pipes Available | Angular 1.x | Angular 4.0 |
currency | P | P |
date | P | P |
uppercase | P | P |
lowercase | P | P |
Json | P | P |
limitTo | P | P |
Number | P | |
orderBy | P | |
Filter | P | |
async | | P |
decimal | | P |
percent | | P |
So we can called Pipes as an modernized version of Filters in Angular 1.x.
Why Pipes?
Actually, Pipes do not give any new feature in our template. In Angular 4.0, we can use logic in the templates also. We can also execute or called a function to obtain the desired value in the template. Basically, pipes provide a sophisticated and handsome way to perform the above task within the templates. Basically, pipes make our code clean and structured.
Syntax of Pipes
- myValue | myPipe:param1:param2 | mySecondPipe:param1
The pipe expression or syntax starts with the value followed by the symbol pipe (|), then the pipe name. The params for that pipe can be sent separated by colon (:) symbol. The order of execution is from left to right. However, the pipe operator only works in your templates and not in JavaScript code. In JavaScript, the pipe symbol works as a bitwise operator.
Uses of Pipes
- We can display only some filtered elements from an array.
- We can modify or format the value.
- We can use them as a function.
- We can do all of the above combined.
Filters vs Pipes
In Angular 1.x, filters act as helpers, actually very similar to functions where we can pass the input and other parameters and it returns a formatted value. But in Angular 4.0, pipes work as an operator. The basic idea is, we have an input and we can modify the input applying more than one pipe in it. This not only simplifies the nested pipe logic but also gave us a beautiful and clean syntax for our templates. Secondly, in case of async operations (which is newly introduced in angular 4.0, we will later in this chapter), we need to set things manually in case of angular 1.X filters. But pipes are smart enough to handle async operations.
Basic Pipes
Most of the pipes provided by Angular 4.0 will be familiar with us if we already worked in Angular 1.x. Actually, pipes do not provide any new feature in Angular 4.0. In Angular 4.0, we can use logic in the template. Also, we can execute or fire any function within the template to obtain its return value. The pipe syntax starts with the actual input value followed by the pipe (|) symbol and then the pipe name. the parameters of that pipe can be sent separately by the colon (;) symbol. The order of execution of a pipe is right to left. Normally, pipes work within our template and not in JavaScript code.
New Pipes