Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the filters (pipes) in components in Angular 2 (not in DOM using pipes)

In angular 1 we are having filters, those filters can be used in both DOM and Typescript/Javascript. In Angular 2 we use pipes to do that stuff, but the pipes are accessible to use only in DOM. Is there any other way to use the pipe function in Typescipt (components)? Please help if anyone knows about this.

Example:

<div *for="let item of items">
    {{item.price | priceFilter }}
</div>

I created the user defined pipe called priceFilter but i want to do the same filtering in Javascript/Typescript.

like image 564
sethu palaniyappan Avatar asked Sep 23 '16 11:09

sethu palaniyappan


1 Answers

You can use pipe to filter data in component like this:

let value = new PriceFilterPipe().transform(this.item.price);

I assumed that name of your exported pipe class is PriceFilterPipe. Of course, you need to import PriceFilterPipe in your component as well.

like image 112
Stefan Svrkota Avatar answered Nov 03 '22 01:11

Stefan Svrkota