Reputation: 1370
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?
Upvotes: 3
Views: 2359
Reputation: 9810
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;
}
}
Upvotes: 5
Reputation: 1148
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>
Upvotes: 1
Reputation: 3268
Yeah I had a similar issue but I was able to solve it by creating a NotificationPipe. The NotificationPipe then uses observers to push the data out.
Here is the NotificationPipe`:
import { Pipe, PipeTransform } from '@angular/core'
import { MessageService } from '../../../services'
import { Issue, Action } from '../../../model/issue/issue'
@Pipe({ name: 'issueNotification' })
export class IssueNotificationPipe implements PipeTransform {
constructor(private messageService: MessageService, ) {
}
transform(issues: Issue[]): Issue[] {
this.messageService.sendMessage({ 'action': Action.BOOKMARKS_FILTERED, 'msg': issues })
return issues
}
}
MessageService is just a service wrapper that abstract the Observers
Upvotes: 0