Marcin Jahn | Dev Notebook
  • Home
  • Programming
  • Technologies
  • Projects
  • About
  • Home
  • Programming
  • Technologies
  • Projects
  • About
  • An icon of the Core section Core
    • Programs Execution
    • Stack and Heap
    • Asynchronous Programming
      • Overview
      • Event Queues
      • Fibers
      • Stackless Coroutines
  • An icon of the .NET section .NET
    • HTTPClient
    • Async
      • How Async Works
      • TAP Tips
    • Equality
    • Comparisons
    • Enumerables
    • Unit Tests
    • Generic Host
    • Logging
    • Configuration
    • Records
    • Nullability
    • Garbage Collector
    • IL and Allocations
    • gRPC
    • Source Generators
    • Platform Invoke
    • ASP.NET Core
      • Overview
      • Middleware
      • Razor Pages
      • Routing in Razor Pages
      • Web APIs
      • Filters
      • Identity
      • Validation
      • Tips
    • Entity Framework Core
      • Overview
      • Testing
      • Tips
  • An icon of the Angular section Angular
    • Overview
    • Components
    • Directives
    • Services and DI
    • Routing
    • Observables (RxJS)
    • Forms
    • Pipes
    • HTTP
    • Modules
    • NgRx
    • Angular Universal
    • Tips
    • Standalone Components
  • An icon of the JavaScript section JavaScript
    • OOP
    • JavaScript - The Weird Parts
    • JS Functions
    • ES Modules
    • Node.js
    • Axios Tips
    • TypeScript
      • TypeScript Environment Setup
      • TypeScript Tips
    • React
      • React Routing
      • MobX
    • Advanced Vue.js Features
  • An icon of the Rust section Rust
    • Overview
    • Cargo
    • Basics
    • Ownership
    • Structures
    • Enums
    • Organization
    • Collections
    • Error Handling
    • Generics
    • Traits
    • Lifetimes
    • Closures
    • Raw Pointers
    • Smart Pointers
    • Concurrency
    • Testing
    • Tips
  • An icon of the C/C++ section C/C++
    • Compilation
    • Structures
    • OOP in C
    • Pointers
    • Strings
    • Dynamic Memory
    • argc and argv Visualization
  • An icon of the GTK section GTK
    • Overview
    • GObject
    • GJS
  • An icon of the CSS section CSS
    • Responsive Design
    • CSS Tips
    • CSS Pixel
  • An icon of the Unity section Unity
    • Unity
  • An icon of the Functional Programming section Functional Programming
    • Fundamentals of Functional Programming
    • .NET Functional Features
    • Signatures
    • Function Composition
    • Error Handling
    • Partial Application
    • Modularity
    • Category Theory
      • Overview
      • Monoid
      • Other Magmas
      • Functors
  • An icon of the Algorithms section Algorithms
    • Big O Notation
    • Array
    • Linked List
    • Queue
    • Hash Table and Set
    • Tree
    • Sorting
    • Searching
  • An icon of the Architecture section Architecture
    • What is architecture?
    • Domain-Driven Design
    • ASP.NET Core Projects
  • An icon of the Core section Core
    • Programs Execution
    • Stack and Heap
    • Asynchronous Programming
      • Overview
      • Event Queues
      • Fibers
      • Stackless Coroutines
  • An icon of the .NET section .NET
    • HTTPClient
    • Async
      • How Async Works
      • TAP Tips
    • Equality
    • Comparisons
    • Enumerables
    • Unit Tests
    • Generic Host
    • Logging
    • Configuration
    • Records
    • Nullability
    • Garbage Collector
    • IL and Allocations
    • gRPC
    • Source Generators
    • Platform Invoke
    • ASP.NET Core
      • Overview
      • Middleware
      • Razor Pages
      • Routing in Razor Pages
      • Web APIs
      • Filters
      • Identity
      • Validation
      • Tips
    • Entity Framework Core
      • Overview
      • Testing
      • Tips
  • An icon of the Angular section Angular
    • Overview
    • Components
    • Directives
    • Services and DI
    • Routing
    • Observables (RxJS)
    • Forms
    • Pipes
    • HTTP
    • Modules
    • NgRx
    • Angular Universal
    • Tips
    • Standalone Components
  • An icon of the JavaScript section JavaScript
    • OOP
    • JavaScript - The Weird Parts
    • JS Functions
    • ES Modules
    • Node.js
    • Axios Tips
    • TypeScript
      • TypeScript Environment Setup
      • TypeScript Tips
    • React
      • React Routing
      • MobX
    • Advanced Vue.js Features
  • An icon of the Rust section Rust
    • Overview
    • Cargo
    • Basics
    • Ownership
    • Structures
    • Enums
    • Organization
    • Collections
    • Error Handling
    • Generics
    • Traits
    • Lifetimes
    • Closures
    • Raw Pointers
    • Smart Pointers
    • Concurrency
    • Testing
    • Tips
  • An icon of the C/C++ section C/C++
    • Compilation
    • Structures
    • OOP in C
    • Pointers
    • Strings
    • Dynamic Memory
    • argc and argv Visualization
  • An icon of the GTK section GTK
    • Overview
    • GObject
    • GJS
  • An icon of the CSS section CSS
    • Responsive Design
    • CSS Tips
    • CSS Pixel
  • An icon of the Unity section Unity
    • Unity
  • An icon of the Functional Programming section Functional Programming
    • Fundamentals of Functional Programming
    • .NET Functional Features
    • Signatures
    • Function Composition
    • Error Handling
    • Partial Application
    • Modularity
    • Category Theory
      • Overview
      • Monoid
      • Other Magmas
      • Functors
  • An icon of the Algorithms section Algorithms
    • Big O Notation
    • Array
    • Linked List
    • Queue
    • Hash Table and Set
    • Tree
    • Sorting
    • Searching
  • An icon of the Architecture section Architecture
    • What is architecture?
    • Domain-Driven Design
    • ASP.NET Core Projects

Pipes in Angular work a bit similarly to UNIX shell pipes. They take some data in an transform it into something else on the output.

Here’s a quick example:

<p>{{ name | uppercase }}</p>

The name is a property defined within the conponent. The uppercase token is a pipe. It’s built in Angular and it turns the input into an uppercase string. The source of the string (component’s property) does not get modified, it’s just displayed uppercase in the browser.

Chaining

We can chain multiple pipes together to apply some modifications sequentially.

Not only strings

Pipes might work on any kind of data, not only strings. Additionally, they are not only applicable to {{ }} outputs, but also to ngFor. For example, we could use a pipe to filter some elements of the array.

There are a bunch of built-in pipes, and we can also create our own.

Parameters

Some pipes support configuration via parameters. Here’s an example of how to use it:

<p>{{ endDate | date:'shortTime' }}</p>

The DatePipe pipe accepts format parameter. In this case, we used the “shortTime” format.

More parameters

If there are more paremeters, we separate them with colons (:).

The parameters may be dynamic, meaning we can use some variable as a value for the parameter.

Custom Pipes

The file name convention is to postfix it with pipe.ts.

Here’s an example of a pipe that truncates inputs:

@Pipe({
standalone: true,
name: 'truncate'
})
export class TruncatePipe implements PipeTransform {
transform(input: any, length = 3) {
if (input.length <= length) {
return input;
}
return input.substr(0, length) + '...';
}
}

Any pipe needs to return string data. This pipe accepts an optional parameter - length.

Non-standalone pipes should be registered in module’s declarations:

@NgModule({
declarations: [
AppComponent,
TruncatePipe
],
...
})
export class AppModule

The custom pipe can be used just like any other pipe:

<p>{{ description | truncate:50 }}</p>

ng CLI

We can use ng CLI to generate a new pipe: ng g p <name>.

Async Pipe

There is a built-in pipe that is useful for Promise<T> and Observable<T>. It displays the value only when it’s resolved. Until then, it will display an empty string. It’s useufl for displaying the results of HTTP requests.

<p>{{ dataFromApi | async }}</p>

Impure Pipes

By default, pipes are pure. Angular docs say that pipes use a simplified change detection mechanism. This matters when we apply a pipe to some composite object. Pipes will not recalculate the output when the content of composite objects changes (like changing a property of input object, or changing the amount of items in an input array). This improves performance. We can disable that optimization on our pipes with pure: false added in the decorator of the pipe. With that, our pipe will be rerun whenever a composite object changes.

Note that the default change detection will work if we change the reference of the input composite object (e.g., pointing the variable to a different array).

←  Forms
HTTP  →
© 2023 Marcin Jahn | Dev Notebook | All Rights Reserved. | Built with Astro.