← All posts

The Unknown Angular Pipes You Need to Start Using

Photo by the blowup on Unsplash

If you’re an Angular developer, you probably know that pipes are a powerful tool that can help you transform and format data in your templates. But did you know that Angular comes with several built-in pipes that you might not be using to their full potential? In this article, we’ll take a closer look at some of these lesser-known pipes and explore how you can use them to make your Angular applications even better.

There are all possible built-in pipes:

  • Async pipe
  • Currency pipe
  • Date pipe
  • Decimal pipe
  • Percent pipe
  • Json pipe
  • Slice pipe
  • Lowercase pipe
  • Uppercase pipe
  • Titlecase pipe
  • KeyValue pipe

1. Async Pipe

The async pipe is a built-in pipe that helps you handle asynchronous data in your templates. When you use the async pipe, Angular automatically subscribes to an observable or a promise and returns the latest value emitted by the observable or the resolved value of the promise. This means you don't have to manually subscribe and unsubscribe to the observable or promise, which can help you avoid memory leaks and simplify your code.
Documentation

<h1>Latest price: {{ price$ | async }}</h1>

In this example, price$ is an Observable that emits the latest price of a product. The async pipe subscribes to this Observable and retrieves its emitted value, which is then displayed in the template.

2. Currency Pipe

The currency pipe is a built-in pipe that helps you format currency values in your templates. When you use the currency pipe, you can specify the currency symbol, the number of decimal places, and the locale. The pipe automatically formats the value according to the user's locale, making it easy to display currency values in a way that makes sense to your users.
Documentation

<h1>Price: {{ price | currency:'USD':'symbol' }}</h1>

In this example, price is a number that represents the price of a product. The currency pipe formats this number as a currency using the USD currency code and the symbol format.

3. Date Pipe

The date pipe is a built-in pipe that helps you format dates in your templates. When you use the date pipe, you can specify the format of the date and the timezone. The pipe automatically formats the date according to the user's locale, making it easy to display dates in a way that makes sense to your users.
Documentation

<p>Date: {{ lastUpdated | date:'yyyy-MM-dd' }}</p>

In this example, lastUpdated is a date that represents the last time a product was updated. The date pipe formats this date using the ‘yyyy-MM-dd’ format.

4. Decimal Pipe

Formats a value according to digit options and locale rules. Locale determines group sizing and separator, decimal point character, and other locale-specific configurations.
Documentation

{{ value_expression | number [ : digitsInfo [ : locale ] ] }}

5. Percent Pipe

The percent pipe is a built-in pipe that helps you format percentage values in your templates. When you use the percent pipe, you can specify the number of decimal places and the locale. The pipe automatically formats the value according to the user's locale, making it easy to display percentage values in a way that makes sense to your users.
Documentation

<p>Percent: {{ myNumber | percent }}</p>

In this example, myNumber is a variable that contains a number. The percent pipe formats the number as a percentage and displays it in the template.

6. JSON Pipe

The json pipe is a built-in pipe that helps you display JSON data in your templates. When you use the json pipe, it formats the JSON data as a string so you can easily display it in your template.
Documentation

<p>Object as JSON: {{ myJsonData | json }}</p>

In this example, myJsonData is a variable that contains JSON data. The json pipe formats the data and displays it in a pre tag.

7. Slice Pipe

The slice pipe is a built-in pipe that helps you extract a subset of an array in your templates. When you use the slice pipe, you can specify the start and end indexes of the subset you want to extract.
Documentation

<p>Substring: {{ myText | slice:0:5 }}</p>

In this example, myText is a variable that contains a string. The slice pipe extracts the first five characters of the string and displays them in the template.

8. Uppercase Pipe

The uppercase pipe is a built-in pipe that helps you convert text to uppercase in your templates.
Documentation

<p>Uppercase: {{ myText | uppercase }}</p>

In this example, myText is a variable that contains a string. The uppercase pipe converts the string to uppercase and displays it in the template.

9. Lowercase Pipe

The lowercase pipe is a built-in pipe that helps you convert text to lowercase in your templates.
Documentation

<p>Lowercase: {{ myText | lowercase }}</p>

In this example, myText is a variable that contains a string. The lowercase pipe converts the string to lowercase and displays it in the template.

10. Titlecase Pipe

The titlecase pipe is a built-in pipe that helps you convert text to title case in your templates. In title case, the first letter of each word is capitalized, except for articles, conjunctions, and prepositions (unless they are the first word).
Documentation

<p>Title case: {{ myText | titlecase }}</p>

In this example, myText is a variable that contains a string. The titlecase pipe converts the string to title case and displays it in the template.

11. KeyValue Pipe

Transforms Object or Map into an array of key value pairs. The output array will be ordered by keys. By default the comparator will be by Unicode point value. You can optionally pass a compareFn if your keys are complex types.
Documentation

<div *ngFor="let item of object | keyvalue">
{{item.key}}:{{item.value}}
</div>

This examples show how an Object or a Map can be iterated by ngFor with the use of this keyvalue pipe.

Conclusion

Using built-in pipes can help you write cleaner and more readable code, by separating the presentation logic from the business logic. By using pipes, you can transform and format data in your templates without cluttering your component code with formatting and display logic. This makes your code more maintainable and easier to test.