Unlock access to the largest independent learning library in Tech for FREE!
Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
Renews at $19.99/month. Cancel anytime
So now that we know how to use a currency pipe operator, let's put together an example to display multiple currency and country formats.
The following is the complete component class, which implements a currency pipe operator:
import { Component } from '@angular/core';
@Component({
selector: 'currency-pipe', template: `
<h5>Built-In CurrencyPipe</h5>
<ol>
<li>
<p>Salary in USD: {{ salary | currency:'USD':true }}</p>
<p>Expenses in INR: {{ expenses | currency:'INR':false }}</p>
</li>
</ol>
`
})
export class CurrencyPipeComponent {
salary: number = 2500;
expenses: number = 1500;
}
Let's analyze the the preceding code in detail:
- We created a component class, CurrencyPipeComponent, and declared few variables, namely salary and expenses.
- In the component template, we transformed the display of the variables by adding the country and currency details.
- In the first pipe operator, we used 'currency : USD', which will append the ($) dollar symbol before the variable.
- In the second pipe operator, we used 'currency : 'INR':false', which will add the currency code, and false will tell not to print the symbol.
Launch the app, and we should see the output as shown in the following screenshot:
In this section, we learned about and implemented CurrencyPipe. In the following sections, we will keep exploring and learning about other Built-in Pipes and much more.
import { Component } from '@angular/core';
@Component({
selector: 'currency-pipe', template: `
<h5>Built-In CurrencyPipe</h5>
<ol>
<li>
<p>Salary in USD: {{ salary | currency:'USD':true }}</p>
<p>Expenses in INR: {{ expenses | currency:'INR':false }}</p>
</li>
</ol>
`
})
export class CurrencyPipeComponent {
salary: number = 2500;
expenses: number = 1500;
}
The LowercasePipe and UppercasePipe, as the name suggests, help in transforming the text into lowercase and uppercase, respectively.
Take a look at the following code snippet:
Author is Lowercase
{{authorName | lowercase }}
Author in Uppercase is
{{authorName | uppercase }}
Let's analyze the preceding code in detail:
- The first line of code transforms the value of authorName into a lowercase using the lowercase pipe
- The second line of code transforms the value of authorName into an uppercase using the uppercase pipe.
Now that we saw how to define lowercase and uppercase pipes, it's time we create a complete component example, which implements the Pipes to show author name in both lowercase and uppercase.
Take a look at the following code snippet:
import { Component } from '@angular/core';
@Component({
selector: 'textcase-pipe', template: `
<h5>Built-In LowercasPipe and UppercasePipe</h5>
<ol>
<li>
<strong>LowercasePipe example</strong>
<p>Author in lowercase is {{authorName | lowercase}}
</li>
<li>
<strong>UpperCasePipe example</strong>
<p>Author in uppercase is {{authorName | uppercase}}
</li>
</ol>
`
})
export class TextCasePipeComponent {
authorName = "Sridhar Rao";
}
Let's analyze the preceding code in detail:
- We create a component class, TextCasePipeComponent, and define a variable authorName.
- In the component view, we use the lowercase and uppercase pipes.
- The first pipe will transform the value of the variable to the lowercase text.
- The second pipe will transform the value of the variable to uppercase text.
Run the application, and we should see the output as shown in the following screenshot:
In this section, you learned how to use lowercase and uppercase pipes to transform the values.
JSON Pipe
Similar to JSON filter in Angular 1.x, we have JSON Pipe, which helps us transform the string into a JSON format string.
In lowercase or uppercase pipe, we were transforming the strings; using JSON Pipe, we can transform and display the string into a JSON format string.
The general syntax is shown in the following code snippet:
<pre>{{ myObj | json }}</pre>
Now, let's use the preceding syntax and create a complete component example, which uses the JSON Pipe:
import { Component } from '@angular/core';
@Component({
template: `
<h5>Author Page</h5>
<pre>{{ authorObj | json }}</pre>
`
})
export class JSONPipeComponent {
authorObj: any; constructor() { this.authorObj = { name: 'Sridhar Rao',
website: 'http://packtpub.com', Books: 'Mastering Angular2'
};
}
}
Let's analyze the preceding code in detail:
- We created a component class JSONPipeComponent and authorObj and assigned the JSON string to the variable.
- In the component template view, we transformed and displayed the JSON string.
Run the app, and we should see the output as shown in the following screenshot:
JSON is soon becoming defacto standard of web applications to integrate between services and client technologies. Hence, JSON Pipe comes in handy every time we need to transform our values to JSON structure in the view.
Slice pipe
Slice Pipe is very similar to array slice JavaScript function. It gets a sub string from a strong certain start and end positions.
The general syntax to define a slice pipe is given as follows:
{{email_id | slice:0:4 }}
In the preceding code snippet, we are slicing the e-mail address to show only the first four characters of the variable value email_id.
Now that we know how to use a slice pipe, let's put it together in a component. The following is the complete complete code snippet implementing the slice pipe:
import { Component } from '@angular/core';
@Component({
selector: 'slice-pipe', template: `
<h5>Built-In Slice Pipe</h5>
<ol>
<li>
<strong>LowercasePipe example</strong>
<p> Email Id is {{ emailAddress }}
</li>
<li>
<strong>LowercasePipe example</strong>
<p>Sliced Email Id is {{emailAddress | slice : 0: 4}}
</li>
</ol>
`
})
export class SlicePipeComponent {
emailAddress = "[email protected]";
}
Let's analyze the preceding code snippet in detail:
- We are creating a class SlicePipeComponent.
- We defined a string variable emailAddress and assign it a value, [email protected].
- Then, we applied the slice pipe to the {{emailAddress | slice : 0: 4}} variable.
- We get the sub string starting 0 position and get four characters from the variable value of emailAddress.
Run the app, and we should the output as shown in the following screenshot:
SlicePipe is certainly a very helpful Built-in Pipe specially dealing with strings or substrings.
async Pipe
async Pipe allows us to directly map a promises or observables into our template view. To understand async Pipe better, let me throw some light on an Observable first.
Observables are Angular-injectable services, which can be used to stream data to multiple sections in the application. In the following code snippet, we are using async Pipe as a promise to resolve the list of authors being returned:
<ul id="author-list">
<li *ngFor="let author of authors | async">
<!-- loop the object here -->
</li>
</ul>
The async pipe now subscribes to the observable (authors) and retrieve the last value. Let's look at examples of how we can use the async pipe as both promise and an observable.
Add the following lines of code in our app.component.ts file:
getAuthorDetails(): Observable<Author[]> {
return this.http.get(this.url).map((res: Response) => res.json());
}
getAuthorList(): Promise<Author[]> {
return this.http.get(this.url).toPromise().then((res: Response) =>
res.json());
}
Let's analyze the preceding code snippet in detail:
- We created a method called getAuthorDetails and attached an observable with the same. The method will return the response from the URL--which is a JSON output.
- In the getAuthorList method, we are binding a promise, which needs to be resolved or rejected in the output returned by the url called through a http request.
In this section, we have seen how the async pipe works. You will find it very similar to dealing with services. We can either map a promise or an observable and map the result to the template.
To summarize, we demonstrated Angular Pipes by explaining in detail about various built-in Pipes such as DatePipe, DecimalPipe, CurrencyPipe, LowercasePipe and UppercasePipe, JSON Pipe, SlicePipe, and async Pipe.
[box type="note" align="" class="" width=""]The above article is an excerpt from the book Expert Angular, written by Sridhar Rao, Rajesh Gunasundaram, and Mathieu Nayrolles. This book will help you learn everything you need to build highly scalable and robust web applications using Angular 4. What are you waiting for, check out the book now to become an expert Angular developer![/box]
Get Familiar with Angular
Interview - Why switch to Angular for web development
Building Components Using Angular