Skip to the workspace
Pebellslearn by building
Pipes  ›  Lesson

Chaining Pipes and Precedence

Pipes Precedence 12 minPipes can be chained left to right, and because the pipe operator binds more loosely than arithmetic and logic but more tightly than the ternary operator, parentheses make the intended grouping explicit.

angular / Chaining Pipes and Precedence

Explore each scene at your own pace.

01 · The rule

The rule

Chaining Pipes and Precedence

The shop page in Greenhouse shows a total price and a label. Both expressions look right, and one of them is quietly wrong: the label shows "Discount" in normal case and "FULL PRICE" in capitals. The bug is not in the pipe. It is in which part of the expression the pipe grabbed.

The rule

Pipes run left to right, grab less than maths, more than ?:.

Chaining. You can pass one pipe's output into another. They run left to right:

{{ plant().name | slice:0:12 | titlecase }}

First the name is cut to 12 characters, then its words are capitalised. Order matters. | date | uppercase works: date turns the Date into text, and uppercase accepts text. | uppercase | date fails, because uppercase only accepts strings and a Date is not one. Each pipe must accept what the previous one produced.

Arguments follow colons, one per colon: slice:0:12, date:'d MMM':'UTC'.

Precedence. The pipe operator binds more loosely than arithmetic, comparison, &&, || and ??. So the whole calculation on its left is done first:

{{ price() * count() | currency:'INR' }}   <!-- (price × count), then formatted -->

But it binds more tightly than the ternary ? :. So in a ternary, the pipe takes only the last branch:

{{ sale() ? 'Discount' : 'Full price' | uppercase }}
<!-- same as: sale() ? 'Discount' : ('Full price' | uppercase) -->

The fix is parentheses:

{{ (sale() ? 'Discount' : 'Full price') | uppercase }}
Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.