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

1

6

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.

Banderillero answered 23/9, 2016 at 11:49 Comment(0)
B
9

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.

Bedfellow answered 23/9, 2016 at 11:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.