从角度 2 管道获取数据 [英] Get data from angular 2 pipe

查看:33
本文介绍了从角度 2 管道获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从管道到组件获取过滤数据?

Is it possible to get filetered data from pipe to component?

如果我们的数据通过模板中的管道过滤,我如何获取过滤后的数据并将其传递到我的组件中?:)

If our data filtered by pipes in template, How I can grab this filtered data and pass it into my component ? :)

非常感谢您的帮助.

推荐答案

我不知道这是不是一个好主意以及您的确切用例是什么,但是您可以将组件实例(或共享服务)注入管道构造函数.

I don't know if it's a good think and what is your exact use case but you can inject the component instance (or a shared service) into the pipe constructor.

然后你可以设置它的值:

Then you can set value on it:

@Pipe({
  name: 'test'
})
export class TestPipe {
  constructor(@Inject(forwardRef(() => AppComponent)) private comp:AppComponent) {

  }

  transform(value) {
    var filtered = value.map((v) => v-1);
    this.comp.filteredData = filtered;
    return filtered;
  }
}

查看此插件:https://plnkr.co/edit/Lp6p97FtytdMM6mA7VGX?p=preview.

这篇关于从角度 2 管道获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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