tsadkan yitbarek tsadkan yitbarek - 2 months ago 13
TypeScript Question

Angular 2 store piped value in variable

I have a pipe called search and I items I want to store the returned piped value in avariable like this (In my template)

let searchedItems = items | search


any ideas?

Answer

Well, assuming you are inside a component you could instantiate a new pipe and apply his transformation inline like so:

let searchedItems = new SearchPipe().transform(items);

Also, you can take advantage of Angular2's injection system:

import { SearchPipe} from './pipes';

class SearchService {

    constructor(private searchPipe: SearchPipe) {

    }

    public searchItems(items: any[]): any[]{
        let searchedItems = this.searchPipe.transform(items);

        return searchedItems;
    }
}