tezvyn:

Would you use a Service or a custom Pipe for currency formatting?

AI-drafted, machine-checkedSource: angular.devintermediate
WHAT IT TESTS

Using Pipes for template formatting versus Services.

ANSWER OUTLINE

Choose Pipe for declarative syntax; implement PipeTransform with transform(value, ...args); use Intl.NumberFormat; register standalone.

WHAT THIS TESTS: Angular is designed so that Pipes handle value transformation inside templates while Services handle business logic and data fetching. This question checks whether you know that boundary and can implement a Pipe correctly. It also surfaces whether you understand change detection implications, because a Service method invoked in a template runs on every change detection cycle unless memoized, whereas a pure Pipe caches results. Interviewers want to see that you prioritize declarative template syntax and reusable presentation utilities.

A GOOD ANSWER COVERS: A strong answer picks a custom Pipe without hesitation and justifies it with three points. First, Pipes are the idiomatic Angular tool for formatting data in templates, producing clean declarative markup like price | myCurrency instead of method calls. Second, you should mention implementing the PipeTransform interface and writing a transform method with the signature transform(value: number | string, ...args: any[]): string. Third, inside transform you should delegate to a robust formatter such as Intl.NumberFormat or the built-in CurrencyPipe to handle locale, symbol, and digit grouping rather than reinventing string concatenation. Fourth, note that the Pipe should be registered as standalone or declared in a module so it is available in templates. Fifth, mention purity: keep it pure unless the locale can change dynamically, because pure pipes memoize and avoid unnecessary re-renders.

COMMON WRONG ANSWERS: The biggest red flag is choosing a Service and calling it from the template via a method or getter. That pattern triggers change detection on every cycle, bloats the component class, and forces imperative syntax. Another red flag is implementing the formatting logic directly inside the component instead of extracting it. Some candidates forget the PipeTransform interface or give transform an incorrect signature. A subtle miss is hardcoding locale or currency symbols instead of using the browser or Angular locale APIs.

LIKELY FOLLOW-UPS: The interviewer may ask how you would handle locale changes at runtime, which would push you toward an impure pipe or a signal-based re-computation. They might ask how to test the pipe, expecting you to instantiate it directly and assert on transform output. They could also ask about performance with large lists, where pure pipes shine because Angular caches prior inputs.

ONE CONCRETE EXAMPLE: Imagine a product list with five hundred rows. Using a Service method in the template means five hundred function calls per change detection cycle. With a pure Pipe, Angular only re-runs transform when the price reference changes, cutting CPU time dramatically and keeping the template readable.

Read the original → angular.dev

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.