Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 349
tsadkan yitbarek Avatar asked May 24 '26 07:05

tsadkan yitbarek


2 Answers

Assuming you are inside a component you can instantiate a new pipe and apply its transformation inline like so:

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

In addition, 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;
    }
}
like image 67
lenilsondc Avatar answered May 27 '26 01:05

lenilsondc


store in template:

<input hidden #searchItems="ngModel" [ngModel]="items | search" />
<!--assuming you want to reuse it inside ngFor-->
<li *ngFor="let item of searchItems.value">
 {{item.Name}}
</li>
like image 43
anaval Avatar answered May 27 '26 00:05

anaval