Sub topic 4
Changing Text Case
This chapter covers the three built-in pipes that change the case of text: uppercase, lowercase and titlecase. It assumes you know basic TypeScript, can write Angular components with interpolation, and know how a standalone component lists its dependencies in imports.
What the case pipes do
Each case pipe takes a string and returns a new string. The original value in your component does not change. This keeps the data in its raw form and lets the template decide how to show it.
| Pipe | Input | Output |
|---|---|---|
| `uppercase` | `'hello world'` | `'HELLO WORLD'` |
| `lowercase` | `'Hello World'` | `'hello world'` |
| `titlecase` | `'hello world'` | `'Hello World'` |
The pipes are part of @angular/common. In a standalone component, you do not need to import them one by one in most current Angular versions, because the built-in pipes are available in templates by default. If your project is older, import UpperCasePipe, LowerCasePipe or TitleCasePipe from @angular/common and add them to the component's imports array.
Using them in a template
Apply a pipe with the pipe operator |. The value goes on the left and the pipe name on the right.
import { Component } from '@angular/core';
@Component({
selector: 'app-case-demo',
standalone: true,
template: `
<p>{{ title | uppercase }}</p>
<p>{{ title | lowercase }}</p>
<p>{{ title | titlecase }}</p>
`,
})
export class CaseDemoComponent {
title = 'the quick BROWN fox';
}This component renders three paragraphs:
THE QUICK BROWN FOXthe quick brown foxThe Quick Brown Fox
Notice that titlecase also lowercases the rest of each word. BROWN becomes Brown. So the pipe does not only capitalize the first letter.
How titlecase decides what a word is
The titlecase pipe splits the text on whitespace and capitalizes the first letter of each piece. It then lowercases the remaining letters of that piece.
This leads to some results you may not expect:
'john o\'brien'becomes'John O'brien', because the pipe treatso'brienas a single word.'NASA launch'becomes'Nasa Launch', so acronyms lose their capitals.'state-of-the-art'becomes'State-of-the-art', because the hyphens do not split words.
If you need rules for names, acronyms or small words such as "of" and "the", write a custom pipe. A later chapter shows how.
Handling null and undefined
The case pipes accept null and undefined and return null for them. So a value that is not loaded yet renders as empty text and does not throw an error. Any other non-string value, such as a number or an object, causes a runtime error. Convert such values first or choose a different pipe.
import { Component } from '@angular/core';
@Component({
selector: 'app-user-name',
standalone: true,
template: `<p>Welcome, {{ name | titlecase }}</p>`,
})
export class UserNameComponent {
name: string | null = null;
}With name set to null, the template shows "Welcome, " with nothing after the comma.
Case pipes and CSS
You can often get the same visual result with CSS. The text-transform property offers uppercase, lowercase and capitalize.
| Approach | Changes the text itself | Good for |
|---|---|---|
| Case pipe | Yes, the rendered string changes | Text that is copied, searched, read aloud or compared |
| CSS `text-transform` | No, only its appearance | Purely visual styling such as headings and buttons |
Choose CSS when the case is a design decision that might change. Choose a pipe when the displayed string itself must have that case.
Combining with other pipes
Case pipes work well in a chain, because each pipe receives the result of the one before it. Pipes run from left to right.
<p>{{ createdAt | date:'fullDate' | uppercase }}</p>
<p>{{ user.fullName | lowercase | titlecase }}</p>The second line is redundant, since titlecase already lowercases the rest of each word. Keep chains as short as they can be.
Common mistakes
- Passing a number or object to a case pipe. Only strings,
nullandundefinedare valid. - Expecting
titlecaseto keep acronyms. It lowercases everything after the first letter. - Using a pipe to change the stored value. Pipes only change what is displayed. To change the data, do it in the component class.
- Writing the pipe with parentheses or arguments, such as
uppercase(). The case pipes take no parameters.
Exercise
A component has the property city = 'nEW yORK'. Which template expression shows "New York", and what does {{ city | uppercase }} show?
Show answer
{{ city | titlecase }} shows "New York", because titlecase capitalizes the first letter of each word and lowercases the rest. {{ city | uppercase }} shows "NEW YORK".
Exercise
Why might {{ 'NASA mission' | titlecase }} be a poor choice for a page heading, and what would you use instead?
Show answer
It renders "Nasa Mission", which loses the acronym's capitals. Use a custom pipe that preserves known acronyms, or write the heading text with the correct case and display it without a case pipe.