How do you create a custom pipe? What is the difference between pure and impure pipes?
Model answer
Create a class with the @Pipe decorator, a name, and a transform method that takes the input and returns the output. Add it to the imports of any standalone component that uses it.
A pure pipe, the default, runs only when its input value or reference changes, so it is fast. An impure pipe (pure: false) runs on every change detection cycle and can be expensive. Prefer pure pipes, and create a new array or object instead of mutating when the input changes.
Tips
- Mention the mutation trap with pure pipes.
Learn this topic
Angular Tutorial: Beginner to Advanced .
Sign in to add your own answer.
More questions
- What is a directive? What are the types?
- What is the difference between ngClass and ngStyle, and how do they compare with class and style bindings?
- What is a pipe? Name some built-in pipes.
- What does the async pipe do, and why is it recommended?
- How do you build a custom attribute directive?
- What is the difference between providedIn root and providing a service in a component?