0
0
Angularframework~15 mins

Built-in pipes (date, currency, uppercase) in Angular - Deep Dive

Choose your learning style9 modes available
Overview - Built-in pipes (date, currency, uppercase)
What is it?
Built-in pipes in Angular are simple tools that transform data before showing it on the screen. They let you change how dates, money, or text appear without changing the original data. For example, you can show a date in a friendly format, display a number as currency, or make text uppercase. Pipes help keep your templates clean and your app easy to read.
Why it matters
Without pipes, you would have to write extra code to format data every time you want to show it. This would make your app messy and harder to maintain. Pipes save time and reduce mistakes by reusing formatting logic. They make your app look professional and user-friendly by showing data in the right way automatically.
Where it fits
Before learning pipes, you should understand Angular components and templates basics. After pipes, you can explore creating custom pipes for special formatting needs or learn about Angular directives to control how elements appear.
Mental Model
Core Idea
Pipes are like filters that take raw data and turn it into a nicely formatted display version automatically in your Angular templates.
Think of it like...
Imagine you have a photo that looks dull. A pipe is like a photo filter app that instantly makes the photo look brighter, sharper, or in black and white without changing the original picture.
Template Data Flow:

Raw Data ──▶ [Pipe] ──▶ Formatted Output

Example:

'2024-06-01' ──▶ date pipe ──▶ 'June 1, 2024'

1000 ──▶ currency pipe ──▶ '$1,000.00'

'hello' ──▶ uppercase pipe ──▶ 'HELLO'
Build-Up - 6 Steps
1
FoundationWhat Are Angular Pipes?
🤔
Concept: Introduce the basic idea of pipes as data transformers in templates.
In Angular, a pipe is a simple way to change how data looks when shown in the template. You write {{ value | pipeName }} to apply a pipe. For example, {{ birthday | date }} changes a date value into a readable format.
Result
You see the data formatted nicely in the app without extra code in the component.
Understanding pipes as template helpers helps you separate data logic from display logic, making your code cleaner.
2
FoundationUsing the Date Pipe
🤔
Concept: Learn how to format dates using Angular's built-in date pipe.
The date pipe formats dates into readable strings. You can write {{ myDate | date:'fullDate' }} to show a full date like 'Saturday, June 1, 2024'. You can also use shorter formats like 'shortDate' or custom patterns.
Result
Dates appear in friendly formats that users easily understand.
Knowing date pipe formats saves you from manually parsing and formatting dates in your code.
3
IntermediateFormatting Currency with Currency Pipe
🤔Before reading on: do you think the currency pipe changes the number itself or just how it looks? Commit to your answer.
Concept: Use the currency pipe to display numbers as money with symbols and decimals.
The currency pipe formats numbers as money. For example, {{ amount | currency:'USD':'symbol':'1.2-2' }} shows '$1,234.56'. You can change the currency code, symbol display, and decimal places.
Result
Numbers appear as money with correct symbols and decimals, improving clarity.
Understanding that currency pipe only changes display prevents bugs where data is accidentally altered.
4
IntermediateTransforming Text with Uppercase Pipe
🤔Before reading on: does the uppercase pipe modify the original string or just the displayed text? Commit to your answer.
Concept: Use the uppercase pipe to show text in all capital letters without changing the original data.
The uppercase pipe changes text to uppercase in the template. For example, {{ name | uppercase }} turns 'alice' into 'ALICE'. The original variable stays the same in the component.
Result
Text appears in uppercase on screen, making it stand out or follow style rules.
Knowing pipes only affect display helps keep your data consistent and predictable.
5
AdvancedCombining Multiple Pipes
🤔Before reading on: if you apply two pipes like {{ value | pipeA | pipeB }}, which pipe runs first? Commit to your answer.
Concept: Learn how to chain pipes to apply multiple transformations in order.
You can stack pipes by writing {{ value | pipeA | pipeB }}. The first pipe runs on the raw data, then the second pipe runs on the first pipe's output. For example, {{ amount | currency | uppercase }} formats the number as currency, then makes the text uppercase.
Result
Data is transformed step-by-step, allowing complex formatting in a simple way.
Understanding pipe order helps you predict and control how data appears when multiple changes are needed.
6
ExpertPerformance and Pure Pipes
🤔Before reading on: do you think Angular runs pipes every time change detection runs, or only when input changes? Commit to your answer.
Concept: Explore how Angular optimizes pipes by running pure pipes only when input changes to improve performance.
Built-in pipes like date, currency, and uppercase are pure pipes. Angular calls them only when the input value changes, not on every screen update. This saves CPU time and keeps apps fast. Impure pipes run every time but are slower.
Result
Your app stays responsive even with many pipes because Angular avoids unnecessary work.
Knowing pipe purity helps you write efficient templates and avoid performance pitfalls.
Under the Hood
Angular pipes are functions that take input data and return transformed output. Built-in pipes are implemented as pure functions, meaning they produce the same output for the same input and have no side effects. Angular's change detection system tracks pipe inputs and only recalculates pipe outputs when inputs change, caching results to avoid repeated work.
Why designed this way?
Pipes were designed to separate formatting logic from component code and templates, making code cleaner and easier to maintain. The pure pipe design improves performance by minimizing recalculations during Angular's frequent change detection cycles. Alternatives like impure pipes exist but are slower and used only when necessary.
┌───────────────┐
│ Component Data│
└──────┬────────┘
       │
       ▼
┌───────────────┐
│   Template    │
│  {{ value |   │
│    pipe }}    │
└──────┬────────┘
       │
       ▼
┌───────────────┐
│   Pipe Logic  │
│ (pure function│
│  transforms)  │
└──────┬────────┘
       │
       ▼
┌───────────────┐
│ Formatted Data│
└───────────────┘
Myth Busters - 3 Common Misconceptions
Quick: Does the uppercase pipe change the original string variable in the component? Commit to yes or no.
Common Belief:Applying the uppercase pipe changes the original string variable in the component to uppercase.
Tap to reveal reality
Reality:The uppercase pipe only changes how the string appears in the template; the original variable remains unchanged.
Why it matters:If you assume the original data changes, you might write buggy code that relies on modified data, causing unexpected behavior.
Quick: Does Angular run pure pipes on every change detection cycle regardless of input changes? Commit to yes or no.
Common Belief:Angular runs all pipes every time change detection runs, which can slow down the app.
Tap to reveal reality
Reality:Pure pipes run only when their input changes, improving performance by avoiding unnecessary recalculations.
Why it matters:Believing pipes always run can lead to premature optimization or wrong performance fixes.
Quick: Can you use the currency pipe to convert numbers between currencies automatically? Commit to yes or no.
Common Belief:The currency pipe converts numbers between different currencies based on exchange rates.
Tap to reveal reality
Reality:The currency pipe only formats numbers with currency symbols; it does not perform currency conversion.
Why it matters:Misunderstanding this can cause financial errors if you rely on the pipe for conversion instead of proper logic.
Expert Zone
1
Built-in pipes are pure by default, but you can create impure pipes for dynamic data that changes without input changes, though this impacts performance.
2
The currency pipe respects the user's locale settings, so currency symbols and formats adapt automatically, which is crucial for international apps.
3
Chaining pipes can sometimes cause unexpected results if the output type of one pipe doesn't match the input expected by the next, requiring careful ordering.
When NOT to use
Avoid using pipes for heavy data processing or logic that affects application state. Instead, handle such logic in components or services. For dynamic or frequently changing data that doesn't trigger input changes, consider impure pipes or manual updates, but be aware of performance costs.
Production Patterns
In real apps, built-in pipes are combined with custom pipes to handle specific formatting needs. Pipes are often used in forms, reports, and dashboards to present data clearly. Developers also use pipes with async data streams to format data as it arrives.
Connections
Functional Programming
Pipes are like pure functions that transform data without side effects.
Understanding pipes as pure functions helps grasp why they improve performance and maintainability in Angular.
Localization and Internationalization
Currency and date pipes adapt formatting based on locale settings.
Knowing how pipes handle locale helps build apps that automatically adjust to users' languages and regions.
Data Transformation in ETL Pipelines
Pipes conceptually resemble transformation steps that convert raw data into usable formats.
Seeing pipes as data transformers connects frontend display logic with backend data processing patterns.
Common Pitfalls
#1Trying to change the original data by applying pipes in the template.
Wrong approach:{{ userName | uppercase }}; // expecting userName variable to become uppercase in component
Correct approach:{{ userName | uppercase }}; // only changes display, keep original data unchanged
Root cause:Misunderstanding that pipes only affect template output, not component data.
#2Using impure pipes unnecessarily, causing performance issues.
Wrong approach:Creating an impure pipe for simple formatting like uppercase, which runs on every change detection.
Correct approach:Use built-in pure pipes like uppercase for formatting to benefit from Angular's optimization.
Root cause:Not knowing the difference between pure and impure pipes and their performance impact.
#3Assuming currency pipe converts currency values.
Wrong approach:{{ amount | currency:'EUR' }}; // expecting conversion from USD to EUR
Correct approach:// Perform currency conversion in component or service, then format with currency pipe {{ convertedAmount | currency:'EUR' }}
Root cause:Confusing formatting with data transformation or conversion.
Key Takeaways
Angular built-in pipes transform data for display without changing the original data.
Date, currency, and uppercase pipes help format common data types simply and consistently.
Pipes are pure functions that Angular optimizes by running only when inputs change.
Chaining pipes allows combining multiple transformations in a clear, readable way.
Understanding pipes prevents common mistakes like expecting data mutation or currency conversion.