tsadkan yitbarek
tsadkan yitbarek

Reputation: 1370

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?

Upvotes: 3

Views: 2359

Answers (3)

lenilsondc
lenilsondc

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

anaval
anaval

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

Philip K. Adetiloye
Philip K. Adetiloye

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

Related Questions