How to Pass Multiple Parameters to Pipe in Angular?

By Hardik Savani | April 5, 2020 | Category : Angular


In this post, i will give you example of angular pass multiple parameters to pipe. this example will help you pass multiple parameters to pipe angular. i would like to share with you how to pass multiple parameters to pipe in angular. This article goes in detailed on pass multiple values to pipe angular.

You can easily pass multiple arguments in pipe in angular 6, angular 7, angular 8, angular 9 and angular 10 application.

In this example we will create 'descPipe' custom pipe and create dynamic description with multiple parameters. basically we will create "persons" array and print in table then using custom pipe that will create dynamic description.

You can see bellow simple solution and full example bellow:

Solution:

{{ person.desc | descPipe: 'arg1' : 'arg2' : 'arg3' }}

transform(desc: any, id: any, name: any, website: any): any {

return desc + ': ' + 'Your ID is ' + id + '. Your Name is ' + name + '. Your Website is ' + website + '.';

}

Example

src/app/desc-pipe.pipe.ts

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({

name: 'descPipe'

})

export class DescPipePipe implements PipeTransform {

transform(desc: any, id: any, name: any, website: any): any {

return desc + ': ' + 'Your ID is ' + id + '. Your Name is ' + name + '. Your Website is ' + website + '.';

}

}

Import Pipe: src/app/app.module.ts

import { NgModule } from '@angular/core';

import { BrowserModule } from '@angular/platform-browser';

import { FormsModule } from '@angular/forms';

import { AppComponent } from './app.component';

import { DescPipePipe } from './desc-pipe.pipe';

@NgModule({

imports: [ BrowserModule, FormsModule ],

declarations: [ AppComponent, DescPipePipe ],

bootstrap: [ AppComponent ]

})

export class AppModule { }

Update Component: src/app/app.component.ts

import { Component } from '@angular/core';

@Component({

selector: 'my-app',

templateUrl: './app.component.html',

styleUrls: [ './app.component.css' ]

})

export class AppComponent {

name = 'Angular';

persons = [

{

id: 1,

name: 'Hardik Savani',

gender: 'Male',

website: 'itsolutionstuff.com',

desc: 'Your Info: ',

},

{

id: 2,

name: 'Kajal Patel',

gender: 'Female',

website: 'nicesnippets.com',

desc: 'Your Info: ',

},

{

id: 3,

name: 'Harsukh Makawana',

gender: 'Male',

website: 'laracode.com',

desc: 'Your Info: ',

}

]

}

Use Pipe in HTML: src/app/app.component.html

<h1>How to Pass Multiple Parameters to Pipe in Angular - ItsolutionStuff.com</h1>

<table border="1">

<tr>

<th>ID</th>

<th>Name</th>

<th>Website</th>

<th>Desc</th>

</tr>

<tr *ngFor="let person of persons">

<td>{{ person.id }}</td>

<td>{{ person.name }}</td>

<td>{{ person.website }}</td>

<td>{{ person.desc | descPipe: person.id : person.name : person.website }}</td>

</tr>

</table>

You can see bellow output:

I hope it can help you...




Hardik Savani
My name is Hardik Savani. I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.
Follow Me: Github Twitter
***Do you want me hire for your Project Work? Then Contact US.

We are Recommending you: