Eran Shabi Eran Shabi - 3 months ago 31
Javascript Question

How to use a pipe in a component in Angular 2?

I have a pipe class which returns data based on the arguments you are passing. I know how to use it in my template HTML using the

symbol, but I want to use it in my component too.

Is there a way to call a pipe directly from inside a component or a service in Angular 2?


You can call your pipe directly in your code by using:

YourPipeClass.prototype.transform(value, arg1, arg2);

You can call it from inside your component or from anywhere else that imports it.

There is also the new way:

new SortTodosPipe().transform(value, arg1, arg2);

But keep in mind it will create an object, so either save that object for later use or use the prototype method.

Anyway you choose, you must add the pipe to your providers if you use it inside a component, like so:

    providers: [YourPipe]