Sub topic 4
Changing text case
Angular ships three pipes that change the letter case of a string: uppercase, lowercase and titlecase. They look trivial, but each has specific behavior around null, non-string input, and names or acronyms, and those details cause most of the bugs. This chapter covers how the pipes work and when to use CSS or a custom pipe instead.
This chapter assumes you know basic TypeScript, can write an Angular standalone component with an imports array, and can use interpolation ({{ }}) in a template. It also assumes you know the pipe operator | from the earlier chapters.
The three pipes
All three live in @angular/common. Each is a standalone pipe, so a standalone component lists the pipe class in imports before using it in the template.
| Template name | Class | Effect | Example input | Output |
|---|---|---|---|---|
| `uppercase` | `UpperCasePipe` | Converts every character to upper case | `Angular pipes` | `ANGULAR PIPES` |
| `lowercase` | `LowerCasePipe` | Converts every character to lower case | `Angular PIPES` | `angular pipes` |
| `titlecase` | `TitleCasePipe` | Upper-cases the first letter of each word and lower-cases the rest | `aNGULAR pIPES` | `Angular Pipes` |
None of them takes parameters. Here is a complete component that uses all three:
import { Component } from '@angular/core';
import { LowerCasePipe, TitleCasePipe, UpperCasePipe } from '@angular/common';
@Component({
selector: 'app-case-demo',
standalone: true,
imports: [UpperCasePipe, LowerCasePipe, TitleCasePipe],
template: `
<p>{{ title | uppercase }}</p>
<p>{{ title | lowercase }}</p>
<p>{{ title | titlecase }}</p>
`,
})
export class CaseDemoComponent {
title = 'the quick BROWN fox';
}The template renders these three lines:
THE QUICK BROWN FOX
the quick brown fox
The Quick Brown FoxThe component keeps the original string in title. Only the displayed text changes. This is the main reason to use a pipe here instead of changing the value in the class. The same raw value can be shown in different forms in different places without extra fields.
The import list matters. If you use uppercase in a template and forget UpperCasePipe in imports, Angular reports that the pipe uppercase could not be found. Importing CommonModule also works, but it pulls in far more than you need.
Where you can use them
A pipe works in any template expression, not only in interpolation. Property bindings and control-flow blocks accept it too:
<img [alt]="product.name | titlecase" [src]="product.imageUrl" />
@if (user.role | lowercase; as role) {
<span class="badge badge-{{ role }}">{{ role }}</span>
}
@for (tag of tags; track tag) {
<li>{{ tag | uppercase }}</li>
}The lowercase call in the @if example is a common normalization step. The input might arrive as ADMIN, Admin or admin depending on the API, and the template needs one form to build a CSS class name.
Precedence with the ternary operator
The pipe operator has lower precedence than almost everything else, including the conditional (ternary) operator. This expression does not do what it appears to do:
<p>{{ isAdmin ? 'administrator' : 'user' | uppercase }}</p>The pipe binds only to the final operand 'user', so an administrator sees administrator in lower case. Add parentheses to apply the pipe to the whole result:
<p>{{ (isAdmin ? 'administrator' : 'user') | uppercase }}</p>How the pipes treat their input
Each pipe accepts string | null | undefined. The three cases behave differently:
- Strings are converted and returned.
nullandundefinedreturnnull. Interpolation rendersnullas an empty string, so nothing appears and nothing breaks. You do not need a guard like{{ name ? (name | uppercase) : '' }}.- Anything else throws a runtime error of type
InvalidPipeArgument. A number, a boolean or an object is not silently converted withString().
The third rule catches people who pipe an ID or a code that the API sometimes returns as a number. With strict template type checking turned on, the compiler also rejects a value whose declared type is not string | null | undefined. Convert the value before piping it:
<!-- Fails if orderId is a number -->
<p>{{ orderId | uppercase }}</p>
<!-- Works: convert to string first, in the class -->
<p>{{ orderIdText | uppercase }}</p>Because the pipe returns null for missing values, it works well after the async pipe, which emits null until the first value arrives:
<h2>{{ (title$ | async) | uppercase }}</h2>Before the first emission the heading is empty. After it, the heading shows the upper-cased title.
What titlecase does and does not do
titlecase splits the text into words at whitespace. For each word it upper-cases the first letter and lower-cases every other letter. Consider these inputs:
| Input | Output | Note |
|---|---|---|
| `hello world` | `Hello World` | The typical case |
| `HELLO WORLD` | `Hello World` | Existing capitals are removed |
| `hello-world` | `Hello-world` | A hyphen does not start a new word |
| `o'neil` | `O'neil` | Only the first letter is capitalized |
| `NASA launch` | `Nasa Launch` | Acronyms are damaged |
| `the lord of the rings` | `The Lord Of The Rings` | Small words are capitalized too |
Two rules follow from this table. The pipe lower-cases the rest of each word, so it cannot preserve acronyms or brand names like iPhone (which becomes Iphone). It also has no concept of title-case style rules, so Of and The are capitalized everywhere.
When the built-in behavior is not right, write a custom pipe (covered in its own chapter) instead of working around it in templates.
Pipes versus CSS `text-transform`
CSS can change how text looks without changing the text:
.label {
text-transform: uppercase;
}The two approaches are not interchangeable.
| Concern | Pipe | CSS `text-transform` |
|---|---|---|
| The DOM text | Contains the converted text | Contains the original text |
| Copying text from the page | Copies the converted text | Browsers differ; usually copies the original |
| Searching in the page | Matches the converted text | Matches the original text |
| Screen readers | Read the converted text | Mostly read the original text |
| Reuse in a string, such as a `title` attribute | Works | Does not apply to attributes |
| Locale rules | Uses JavaScript's default case mapping | Can follow the element's `lang` attribute |
Use CSS when the case is purely visual styling, such as a button label that the design shows in capitals. Use a pipe when the converted text is itself the content, for example when it goes into an attribute, a tooltip or a value that users copy.
On locale: the pipes call JavaScript's toUpperCase() and toLowerCase(), which do not read Angular's LOCALE_ID. Setting the locale in the application does not change how uppercase treats a Turkish dotted i. 'ß'.toUpperCase() produces 'SS', so the output can be longer than the input. If you need locale-sensitive case mapping, call toLocaleUpperCase('tr') in a custom pipe or in the component class.
Chaining with other pipes
Case pipes combine with other pipes from left to right. The case pipe must receive a string, so it usually comes after a pipe that produces text:
<p>{{ createdAt | date:'fullDate' | uppercase }}</p>
<p>{{ statusCode | lowercase | titlecase }}</p>Order affects the result. In lowercase | titlecase the first pipe is redundant because titlecase already lower-cases the rest of each word. Putting uppercase before date would fail, because date expects a date value and uppercase returns a string.
Case pipes are pure pipes, as are most built-in pipes. Angular re-runs a pure pipe only when its input reference or value changes, so using them on many list items has a small cost. Pure and impure pipes get their own chapter.
Summary of decisions
- Use
uppercaseorlowercasewhen you need a consistent form for display or for a derived value like a CSS class. - Use
titlecaseonly on plain, known-safe words, and expect it to lower-case everything after the first letter of each word. - Use CSS
text-transformwhen the capitalization is only styling. - Convert non-string values before piping them, and add parentheses when piping the result of an operator expression.
Exercise
A template contains {{ isActive ? 'active' : 'inactive' | uppercase }}. For a component where isActive is true, what does it display, and how do you change the template so it shows ACTIVE?
Show answer
It displays active in lower case. The pipe has lower precedence than the ternary operator, so it applies only to the last operand 'inactive', and the true branch is not converted. Wrap the conditional in parentheses: {{ (isActive ? 'active' : 'inactive') | uppercase }}.
Exercise
A component field code is declared as code: string | number and the template uses {{ code | uppercase }}. The value is sometimes 42. What happens, and what is a safe fix?
Show answer
The uppercase pipe accepts only string, null or undefined. With strict templates the compiler reports a type error. Without that check, the value 42 throws an InvalidPipeArgument error at runtime. Convert the value to a string before piping, for example with a field or computed value codeText = String(this.code) and then {{ codeText | uppercase }}.
Exercise
What does {{ 'NASA mission to mars' | titlecase }} display, and why is that a problem for this input?
Show answer
It displays Nasa Mission To Mars. titlecase upper-cases the first letter of each whitespace-separated word and lower-cases the rest, so the acronym NASA becomes Nasa, and the small word to is also capitalized. For text with acronyms, write a custom pipe or keep the original text.