Search code examples
angularjsangulartypescriptangular-filtersangular-pipe

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.


Solution

  • 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.