Angular 2 将管道值存储在变量中 [英] Angular 2 store piped value in variable

查看:29
本文介绍了Angular 2 将管道值存储在变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 search 的管道,我想将返回的管道值存储在这样的变量中(在我的模板中)

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

有什么想法吗?

推荐答案

假设你在一个组件中,你可以实例化一个新管道并应用它的内联转换,如下所示:

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);

此外,您还可以利用 Angular2 的注入系统:

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;
    }
}

这篇关于Angular 2 将管道值存储在变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆