1

Any rules on when to use a custom pipe and when to use a custom component?

Could those be the rules of thumb (below) ?

Documentation does not seem to answer this question directly: https://angular.io/docs/ts/latest/guide/pipes.html

Could pipes be considered «poor man's components?»


Are there some advantages of pipes over components?

My guesses:

  • shorter syntaxes / expressiveness
  • performance benefits (build-time? run-time?)
  • benefits when working with plain-text?

Does this statement (which is a guess written by me) hold true?

Everything that can be done with a pipe,
can be done (though perhaps at a higher cost) with a component as well?
Community
  • 1
  • 1
KarolDepka
  • 8,318
  • 10
  • 45
  • 58

1 Answers1

1

According to that same documentation:

Pipes transform displayed values within a template

So if you are transforming displayed values, such as formatting a date or filtering a list, then a pipe makes sense.

If you are displaying HTML, use a component.

And the answer from 2015 for using innerHtml is not considered "best practices" and should be limited to only a last resort.

DeborahK
  • 57,520
  • 12
  • 104
  • 129
  • Thanks. Upvoted. Though, if one treats «Pipes transform displayed values within a template» in a very general way, one could say that components also «transform displayed values within a template» (in some very generic sense). Though I understand that there is a line to be drawn, given the criteria. – KarolDepka Apr 18 '17 at 22:35