Angular: Directives vs Pipes

前端 未结 1 1137
庸人自扰
庸人自扰 2021-02-04 01:26

What are the arguments for creating and using directives vs creating and using pipes.

The scenario this question stems from is around currency input an

1条回答
  •  遇见更好的自我
    2021-02-04 01:52

    To bring it to the point in the most simple terms, i would say a pipe is to manipulate data, while a directive is more for DOM manipulation.

    A pipe gets data as an input, transforms it and outputs this data in another way.

    A directive gets a DOM element it's "attached" to and enhances it with some kind of features.

    Of course you will find examples where both make sense (take the Components into account and you have three structure types to decide between) and it's more of a question of preference which you choose.

    In your example you would use a pipe. Let's say you want to show the currency value in bold text and use an image-icon as a currency symbol you probably take a directive

    0 讨论(0)
提交回复
热议问题