Pipes help you change how data looks in your app without changing the data itself. Built-in pipes like date, currency, and uppercase make it easy to show dates, money, and text in a nice way.
Built-in pipes (date, currency, uppercase) in Angular
{{ value | pipeName[:parameter] }}The pipe symbol | is used to apply a pipe to a value in the template.
Some pipes accept optional parameters inside [:parameter] to customize the output.
today date variable to a medium date like 'Jan 1, 2024'.{{ today | date:'mediumDate' }}price as US dollars with a dollar sign and two decimals.{{ price | currency:'USD':'symbol':'1.2-2' }}name text to all uppercase letters.{{ name | uppercase }}This Angular component shows how to use the built-in pipes date, currency, and uppercase. It displays the original values and their formatted versions side by side.
import { Component } from '@angular/core'; @Component({ selector: 'app-pipe-demo', template: ` <h2>Built-in Pipes Demo</h2> <p>Original date: {{ today }}</p> <p>Formatted date: {{ today | date:'fullDate' }}</p> <p>Original price: {{ price }}</p> <p>Formatted price: {{ price | currency:'EUR':'symbol':'1.2-2' }}</p> <p>Original name: {{ name }}</p> <p>Uppercase name: {{ name | uppercase }}</p> ` }) export class PipeDemoComponent { today = new Date(); price = 1234.5; name = 'angular learner'; }
Use pipes only in templates, not in component TypeScript code.
Currency pipe automatically adds commas and decimals based on locale.
Uppercase pipe works only on strings; other types will be ignored.
Built-in pipes format data easily in Angular templates.
Date pipe formats dates in many styles.
Currency pipe shows money with symbols and decimals.
Uppercase pipe changes text to all capital letters.
Practice
uppercase pipe do when applied to a string in a template?Solution
Step 1: Understand the purpose of the uppercase pipe
The uppercase pipe is designed to transform text to all capital letters in Angular templates.Step 2: Compare with other text transformations
Unlike lowercase or capitalize, uppercase changes every letter to uppercase.Final Answer:
Converts all letters in the string to uppercase. -> Option DQuick Check:
uppercase pipe = all caps [OK]
- Confusing uppercase with lowercase pipe
- Thinking it only capitalizes first letter
- Assuming it reverses the string
Solution
Step 1: Recall Angular date pipe format codes
Angular uses lowercase 'yyyy' to represent the full year in date pipe formatting.Step 2: Check each option's format string
{{ myDate | date:'yyyy' }} uses 'yyyy' correctly; A uses uppercase which is invalid; C uses 'year' which is not a valid format; D uses 'yy' which shows two-digit year.Final Answer:
{{ myDate | date:'yyyy' }} -> Option AQuick Check:
Year format = 'yyyy' [OK]
- Using uppercase 'YYYY' instead of 'yyyy'
- Using descriptive words like 'year' instead of format codes
- Using 'yy' which shows only two digits
{{ 1234.5 | currency:'USD':'symbol':'1.2-2' }}What will be the rendered output?
Solution
Step 1: Understand the currency pipe parameters
The currency pipe format is: currency code 'USD', display as symbol '$', and digit info '1.2-2' means minimum 1 digit before decimal, minimum 2 decimals, maximum 2 decimals.Step 2: Apply formatting to the number 1234.5
Number 1234.5 is formatted with commas and two decimals: $1,234.50.Final Answer:
$1,234.50 -> Option AQuick Check:
Currency pipe with '1.2-2' = two decimals [OK]
- Ignoring decimal places and showing one decimal
- Using currency code instead of symbol
- Not adding thousand separators
{{ today | date:'MM-DD-YYYY' }}Solution
Step 1: Check the date format string correctness
Angular date pipe requires lowercase 'yyyy' for year, not uppercase 'YYYY'.Step 2: Verify other parts of the code
The pipe name 'date' is correct, 'today' is assumed defined, and date pipe can format month and day.Final Answer:
The date format string uses uppercase 'YYYY' which is invalid. -> Option CQuick Check:
Date pipe year format = 'yyyy' lowercase [OK]
- Using uppercase 'YYYY' instead of 'yyyy'
- Assuming pipe name is wrong
- Ignoring variable definition issues
Solution
Step 1: Identify the currency code and symbol usage
To show Euros with symbol €, use currency code 'EUR' and display 'symbol'.Step 2: Set decimal places to exactly 3
The digit info '1.3-3' means minimum 1 digit before decimal, exactly 3 decimals.Step 3: Check other options for correctness
{{ price | currency:'EUR':'code':'1.2-2' }} uses 'code' display and 2 decimals; C uses 2 decimals; D uses USD instead of EUR.Final Answer:
{{ price | currency:'EUR':'symbol':'1.3-3' }} -> Option BQuick Check:
Currency EUR with 3 decimals = {{ price | currency:'EUR':'symbol':'1.3-3' }} [OK]
- Using wrong currency code or symbol
- Setting wrong decimal places
- Mixing currency codes (USD instead of EUR)
Ready to prove it?
Pass the test and this pattern moves unknown
