Get data from angular 2 pipe

妖精的绣舞 提交于 2019-12-22 01:16:19

问题


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 ? :)

Many thanks for any help.


回答1:


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

See this plunkr: https://plnkr.co/edit/Lp6p97FtytdMM6mA7VGX?p=preview.



来源:https://stackoverflow.com/questions/36475061/get-data-from-angular-2-pipe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!