0

I am trying to send a filtered array from my pipe to my component, using @Inject(forwardRef(()) like this:

filter.pipe.ts

constructor(@Inject(forwardRef(() => OpenItemsComponent)) private ded_open: OpenItemsComponent) {}
transform(items: any[], field: string, value: string[]): any[] {
    ...
    this.ded_open.filteredData(this.newArray)
    ...
}

app.ts

...
filteredData(val) {
    this.currentFilter = val.slice()
    console.log(this.currentFilter) // in currentFilter I will have the filtered array
...

}

What I am trying to to, is injecting more components, because I use this pipe in 4 components. And I tried something like this:

constructor(
@Inject(forwardRef(() => OpenItemsComponent)) private ded_open: OpenItemsComponent,
@Inject(forwardRef(() => ClosedItemsComponent)) private ded_closed: ClosedItemsComponent,
@Inject(forwardRef(() => TrdIComponent)) private trade_open: TradeInvoicesComponent,
@Inject(forwardRef(() => TrdIClosedComponent)) private trade_closed: TradeInvoicesClosedComponent) {
  }

but I get

ERROR Error: Uncaught (in promise): Error: StaticInjectorError(AppModule)[FilterPipe -> ClosedItemsComponent]: StaticInjectorError(Platform: core)[FilterPipe -> ClosedItemsComponent]: NullInjectorError: No provider for ClosedItemsComponent!

I can't inject more components inside pipe constructor? How can I solve this? Thank you for your time!

(P.S - I know using pipes is bad, but this is just for a demo, not the final version)

Tenzolinho
  • 922
  • 2
  • 15
  • 35
  • 1
    Pipes are used to transform the value **to display it**. It shouldn't change your value. If it does, consider doing your pipe transformation directly into your component. As a sidenote, I'll let you check [this answer](https://stackoverflow.com/questions/48183677/how-to-format-date-in-component-of-angular-5/48183927#48183927) that shows how you can create pipe instances in your components. –  Dec 03 '18 at 09:04
  • I just want to send the filter from pipe to component and this is the way I did.. but it's working only with a component, not with 4, and I don't know why. I can't afford to modify my pipe right now, I am in a hurry. But thank you anyway. – Tenzolinho Dec 03 '18 at 09:18
  • Again, you should not do that. That's not what they're used for. Instead , as said, declare a pipe instance in your component and use its `transform` method. –  Dec 03 '18 at 09:20
  • you could use a service which you inject in both your components and your pipe. (no idea, if it that really works with change detection and everything) But again, it's misusing pipes. Even for a demo. But why not change your pipe to a component and use an `@Output` property? – Arikael Dec 03 '18 at 09:29

0 Answers0