Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 - Passing an argument to a custom pipe

I have a created a pipe which can receive 2 arugments, but I'm not sure how I can sent them.

here's my pipe:

export class TranslationPipe implements PipeTransform {

    private capitalize: boolean;

    constructor(
        private translationService: TranslationService
    ) {
        this.capitalize = true;
    }

    transform(key: string, capitalize?: boolean): string {
        if (typeof capitalize !== "undefined" || capitalize !== null)
            this.capitalize = capitalize;

        return this.translationService.getTranslation(key, this.capitalize);
    }
}

and here is my HTML

{{ 'searchquery' | translate }}

this works, but how can I pass capitlize = false aswell? I've tried some googling but I can't really find any example the way I want to implement it (maybe I'm doing it wrong?)

thanks for your help!

like image 455
Nicolas Avatar asked Mar 09 '17 12:03

Nicolas


People also ask

Can we pass parameters to pipe in angular?

You can easily pass multiple arguments in pipe in angular 6, angular 7, angular 8, angular 9, angular 10, angular 11, angular 12, angular 13 and angular 14 application. In this example we will create 'descPipe' custom pipe and create dynamic description with multiple parameters.

How can you call an angular pipe with multiple arguments?

In your component's template you can use multiple arguments by separating them with colons: {{ myData | myPipe: 'arg1':'arg2':'arg3'... }} Pipes take an array that contains all arguments, so you need to call them like this: new MyPipe().

What is a parameterized pipe in angular?

In an angular application, when we pass any parameters to the pipes, it is called parameterized pipes we can pass n number of parameters to pipes using a colon (:).


1 Answers

{{ 'searchquery' | translate:false }}

{{ 'searchquery' | translate:'toUsEn' }}
like image 196
Meir Avatar answered Oct 16 '22 09:10

Meir