Welcome folks today in this blog post we will be looking the examples
of the built in angular pipes
to format the data such as text,date
and currency
in typescript in browser. All the full source code of the application is shown below.
Get Started
In order to get started you need to make a new angular
project using the below command as shown below
ng new sampleproject
cd sampleproject
And after that you will see the below directory
structure of the angular app as shown below
And now we need to write the code inside the app.component.ts
file as shown below.
app.component.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'], }) export class AppComponent { today = new Date(); cost = 19.99; name = 'John Doe'; percentage = 0.1234; description = 'This is a description of something'; } |
As you can see we have declared the variables
for the data that we will be showing inside the html
template with built in pipes of angular. We have the date
and currency
and then we have some text string
values as well.
And now we need to copy paste the below code inside the app.component.html
file as shown below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<!-- Display the current date using the DatePipe --> <p>Today's date: {{ today | date }}</p> <!-- Format a number as a currency using the CurrencyPipe --> <p>Total cost: {{ cost | currency }}</p> <!-- Transform a string to uppercase using the UppercasePipe --> <p>Name: {{ name | uppercase }}</p> <!-- Format a number with a fixed number of decimal places using the DecimalPipe --> <p>Percentage: {{ percentage | number:'1.2-2' }}</p> <!-- Limit the length of a string using the SlicePipe --> <p>Description: {{ description | slice:0:20 }}...</p> |
As you can see in the above code we are using the interpolation
expression to render the values and inside it we are providing the pipes
that angular have to format the data such as formatting the date
and the currency and we have also pipes to uppercase
and lowercase
the string values. And lastly for decimal values and to slice
the long description.
Creating Custom Pipes
You can execute the below command to generate the custompipe
as shown below
ng g pipe custompipe
Here custompipe
is the name of the pipe. You can replace it with your own name of the pipe.
After this command two files
will be created for the pipe
as shown below
And now you need to go to custompipe.pipe.ts
file and copy paste the following code
custompipe.pipe.ts
1 2 3 4 5 6 7 8 9 10 11 12 |
import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'custompipe' }) export class CustompipePipe implements PipeTransform { transform(value: number): number { return (value*value); } } |
As you can see in the above typescript
code we are simply taking the input
value and then transforming it by calculating the square
of the number and then returning it.
And now to use this custompipe
inside your app.component.html
file you can copy paste the following code
app.component.html
In the template, use the pipe by adding a “|” character followed by the pipe name:
1 |
<p>{{ 5 | custompipe }}</p> |
