![]() |
In Angular, custom pipes are a powerful tool for transforming data in templates. While simple pipes perform basic transformations, there are cases where you may need to pass parameters to customize the behavior of a pipe. Fortunately, Angular provides mechanisms for passing parameters to custom pipes, allowing you to create more versatile and reusable transformations. In this article, we’ll learn how to pass parameters to custom pipes in Angular. What are Custom Pipes?Custom pipes in Angular are classes decorated with Below are the Approaches to create custom pipes: Table of Content Approach 1: Using the NgModule decoratorIn this approach, you can pass the string value to the pipe’s constructor using Angular’s dependency injection system and the @Inject decorator by creating a value in the providers array of the NgModule. Step 1: Generate a new Angular project ng new string-pipe-demo Step 2: Generate a new pipe ng generate pipe reverse Folder Structure: Code Example:
In this example, we’re creating a pipe called ReversePipe that takes a string as input, reverses it, and appends a suffix to the end. Output: ![]() Approach 2: Using Dependency InjectionYou can create a service that holds the string value, inject it into the pipe’s constructor, and then use that value within the pipe’s transformation logic. Step 1: Create a new Service ng generate service value-holding Code Example:
In this approach, we create a SuffixService that holds the string value ‘ Suffix’. We then inject this service into the ReversePipe constructor and use the suffix property within the transform method. Output: |
Reffered: https://www.geeksforgeeks.org
AngularJS |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |