2016-02-06 02:27:06 -05:00
include ../_util-fns
2015-11-10 13:31:46 -05:00
:marked
2015-10-19 12:56:24 -04:00
Every application starts out with what seems like a simple task: get data, transform them, and show them to users.
2015-11-10 13:31:46 -05:00
Getting data could be as simple as creating a local variable or as complex as streaming data over a Websocket.
Once data arrive, we could push their raw `toString` values directly to screen.
That rarely makes for a good user experience.
Almost everyone prefers a simple birthday date
(<span style="font-family:courier">April 15, 1988</span>) to the original raw string format
( <span style="font-family:courier">Fri Apr 15 1988 00:00:00 GMT-0700 (Pacific Daylight Time)</span> ).
2015-10-19 12:56:24 -04:00
Clearly some values benefit from a bit of massage. We soon discover that we
desire many of the same transformations repeatedly, both within and across many applications.
2015-11-10 13:31:46 -05:00
We almost think of them as styles.
2015-10-19 12:56:24 -04:00
In fact, we'd like to apply them in our HTML templates as we do styles.
2015-11-10 13:31:46 -05:00
Welcome, Angular pipes, the simple display-value transformations that we can declare in our HTML!
2015-11-25 20:44:48 -05:00
2015-12-13 01:01:46 -05:00
[Live Example](/resources/live-examples/pipes/ts/plnkr.html).
2015-10-19 12:56:24 -04:00
.l-main-section
2015-11-10 13:31:46 -05:00
:marked
2015-10-19 12:56:24 -04:00
## Using Pipes
2015-11-10 13:31:46 -05:00
A pipe takes in data as input and transforms it to a desired output.
We'll illustrate by transforming a component's birthday property into
2015-12-13 01:01:46 -05:00
a human-friendly date:
+makeExample('pipes/ts/app/hero-birthday1.component.ts', null, 'app/hero-birthday1.component.ts')
2015-11-02 03:28:38 -05:00
2015-11-10 13:31:46 -05:00
:marked
2015-12-13 01:01:46 -05:00
Focus on the component's template.
+makeExample('pipes/ts/app/app.component.html', 'hero-birthday-template')(format=".")
2015-11-10 13:31:46 -05:00
:marked
2015-12-13 01:01:46 -05:00
Inside the interpolation expression we flow the component's `birthday` value through the
[pipe operator](./template-syntax.html#pipe) ( | ) to the [Date pipe](../api/common/DatePipe-class.html)
function on the right. All pipes work this way.
2015-10-19 12:56:24 -04:00
.l-main-section
2015-11-10 13:31:46 -05:00
:marked
2015-10-19 12:56:24 -04:00
## Built-in pipes
2015-11-10 13:31:46 -05:00
Angular comes with a stock set of pipes such as
2015-10-19 12:56:24 -04:00
`DatePipe`, `UpperCasePipe`, `LowerCasePipe`, `CurrencyPipe`, and `PercentPipe`.
They are all immediately available for use in any template.
.l-sub-section
2015-11-10 13:31:46 -05:00
:marked
Learn more about these and many other built-in pipes in the the [API Reference](../api/);
filter for entries that include the word "pipe".
2015-10-19 12:56:24 -04:00
.l-main-section
2015-11-10 13:31:46 -05:00
:marked
2015-10-19 12:56:24 -04:00
## Parameterizing a Pipe
A pipe may accept any number of optional parameters to fine-tune its output.
2015-11-10 13:31:46 -05:00
2015-10-19 12:56:24 -04:00
We add parameters to a pipe by following the pipe name with a colon ( : ) and then the parameter value
2015-10-23 12:31:51 -04:00
(e.g., `currency:'EUR'`). If our pipe accepts multiple parameters, we separate the values with colons (e.g. `slice:1:5`)
2015-11-10 13:31:46 -05:00
2015-12-13 01:01:46 -05:00
We'll modify our birthday template to give the date pipe a format parameter.
After formatting the hero's April 15th birthday should display as **<span style="font-family:courier">04/15/88</span>**.
2015-10-19 12:56:24 -04:00
2015-12-13 01:01:46 -05:00
+makeExample('pipes/ts/app/app.component.html', 'format-birthday')(format=".")
2015-10-19 12:56:24 -04:00
2015-11-10 13:31:46 -05:00
:marked
2015-10-19 12:56:24 -04:00
The parameter value can be any valid
2016-02-12 09:54:42 -05:00
[template expression](./template-syntax.html#template-expressions)
2015-12-13 01:01:46 -05:00
such as a string literal or a component property.
In other words, we can control the format through a binding the same way we control the birthday value through a binding.
Let's write a second component that *binds* the pipe's format parameter
to the component's `format` property. Here's the template for that component:
+makeExample('pipes/ts/app/hero-birthday2.component.ts', 'template', 'app/hero-birthday2.component.ts (template)')(format=".")
2015-11-10 13:31:46 -05:00
:marked
2015-10-19 12:56:24 -04:00
We also added a button to the template and bound its click event to the component's `toggleFormat` method.
That method toggles the component's `format` property between a short form
('shortDate') and a longer form ('fullDate').
2015-12-13 01:01:46 -05:00
+makeExample('pipes/ts/app/hero-birthday2.component.ts', 'class', 'app/hero-birthday2.component.ts (class)')
:marked
2015-10-19 12:56:24 -04:00
As we click the button, the displayed date alternates between
2015-11-02 03:28:38 -05:00
"**<span style="font-family:courier">04/15/1988</span>**" and
2015-10-19 12:56:24 -04:00
"**<span style="font-family:courier">Friday, April 15, 1988</span>**".
2015-11-02 03:28:38 -05:00
figure.image-display
img(src='/resources/images/devguide/pipes/date-format-toggle-anim.gif' alt="Date Format Toggle")
2015-11-10 13:31:46 -05:00
:marked
2015-12-13 01:01:46 -05:00
2015-10-19 12:56:24 -04:00
.l-sub-section
2015-11-10 13:31:46 -05:00
:marked
2015-10-19 12:56:24 -04:00
Learn more about the `DatePipes` format options in the [API Docs](../api/core/DatePipe-class.html).
2015-11-10 13:31:46 -05:00
:marked
2015-10-19 12:56:24 -04:00
## Chaining pipes
2015-11-10 13:31:46 -05:00
We can chain pipes together in potentially useful combinations.
2015-10-19 12:56:24 -04:00
In the following example, we chain the birthday to the `DatePipe` and on to the `UpperCasePipe`
2015-11-10 13:31:46 -05:00
so we can display the birthday in uppercase. The following birthday displays as
2015-10-19 12:56:24 -04:00
**<span style="font-family:courier">APR 15, 1988</span>**
2015-11-02 03:28:38 -05:00
2015-12-13 01:01:46 -05:00
+makeExample('pipes/ts/app/app.component.html', 'chained-birthday')(format=".")
2015-11-02 03:28:38 -05:00
2015-11-10 13:31:46 -05:00
:marked
2015-10-19 12:56:24 -04:00
If we pass a parameter to a filter, we have to add parentheses
to help the template compiler with the evaluation order.
The following example displays
2015-11-10 13:31:46 -05:00
**<span style="font-family:courier">FRIDAY, APRIL 15, 1988</span>**
2015-11-02 03:28:38 -05:00
2015-12-13 01:01:46 -05:00
+makeExample('pipes/ts/app/app.component.html', 'chained-parameter-birthday')(format=".")
2015-10-19 12:56:24 -04:00
.l-sub-section
2015-12-13 01:01:46 -05:00
:marked
We can add parentheses to alter the evaluation order or
to provide extra clarity:
+makeExample('pipes/ts/app/app.component.html', 'chained-parameter-birthday-parens')(format=".")
:marked
2015-10-19 12:56:24 -04:00
.l-main-section
2015-11-10 13:31:46 -05:00
:marked
2015-10-19 12:56:24 -04:00
## Custom Pipes
2015-11-02 03:28:38 -05:00
We can write our own custom pipes.
2015-11-10 13:31:46 -05:00
2015-12-13 01:01:46 -05:00
Here's a custom pipe named `ExponentialStrengthPipe` that can boost a hero's powers:
2015-10-19 12:56:24 -04:00
2015-12-13 01:01:46 -05:00
+makeExample('pipes/ts/app/exponential-strength.pipe.ts', null, 'app/exponential-strength.pipe.ts')
2015-11-10 13:31:46 -05:00
:marked
2015-11-21 21:05:50 -05:00
This pipe definition reveals several key points
2015-12-13 01:01:46 -05:00
* A pipe is a class decorated with pipe metadata.
2015-12-28 14:52:41 -05:00
* The pipe class implements the `PipeTransform` interface's `transform` method that
2015-12-13 01:01:46 -05:00
takes an input value and an optional array of parameter strings and returns the transformed value.
* There will be one item in the parameter array for each parameter passed to the pipe
* We tell Angular that this is a pipe by applying the
`@Pipe` decorator which we import from the core Angular library.
2015-10-19 12:56:24 -04:00
* The `@Pipe` decorator takes an object with a name property whose value is the
2015-11-17 13:17:14 -05:00
pipe name that we'll use within a template expression. It must be a valid JavaScript identifier.
2016-01-15 15:30:00 -05:00
Our pipe's name is `exponentialStrength`.
2015-12-13 01:01:46 -05:00
2015-12-28 14:52:41 -05:00
.l-sub-section
:marked
### The *PipeTransform* Interface
2015-12-13 01:01:46 -05:00
2015-12-28 14:52:41 -05:00
The `transform` method is essential to a pipe.
The `PipeTransform` interface defines that method and guides both tooling and the compiler.
It is optional; Angular looks for and executes the `transform` method regardless.
:marked
2015-12-13 01:01:46 -05:00
Now we need a component to demonstrate our pipe.
+makeExample('pipes/ts/app/power-booster.component.ts',null,'app/power-booster.component.ts')
2015-11-02 03:28:38 -05:00
figure.image-display
img(src='/resources/images/devguide/pipes/power-booster.png' alt="Power Booster")
2015-10-17 13:01:41 -04:00
2015-11-10 13:31:46 -05:00
:marked
2015-10-23 12:31:51 -04:00
Two things to note:
2015-12-13 01:01:46 -05:00
1. We use our custom pipe the same way we use the built-in pipes.
2015-11-10 13:31:46 -05:00
2015-10-19 12:56:24 -04:00
1. We must list our pipe in the @Component decorator's `pipes` array.
2015-11-10 13:31:46 -05:00
2015-12-13 01:01:46 -05:00
.callout.is-helpful
2015-10-19 12:56:24 -04:00
header Remember the pipes array!
2015-11-10 13:31:46 -05:00
:marked
Angular reports an error if we neglect to list our custom pipe.
2015-10-19 12:56:24 -04:00
We didn't list the `DatePipe` in our previous example because all
2015-11-10 13:31:46 -05:00
Angular built-in pipes are pre-registered.
2015-10-19 12:56:24 -04:00
Custom pipes must be registered manually.
2015-11-10 13:31:46 -05:00
:marked
2015-12-13 01:01:46 -05:00
If we try the [live code](/resources/live-examples/pipes/ts/plnkr.html) example,
2015-10-19 12:56:24 -04:00
we can probe its behavior by changing the value and the optional exponent in the template.
2015-11-10 13:31:46 -05:00
2015-10-19 12:56:24 -04:00
## Power Boost Calculator (extra-credit)
It's not much fun updating the template to test our custom pipe.
We could upgrade the example to a "Power Boost Calculator" that combines
2015-12-13 01:01:46 -05:00
our pipe and two-way data binding with `ngModel`.
2015-10-19 12:56:24 -04:00
2015-12-13 01:01:46 -05:00
+makeExample('pipes/ts/app/power-boost-calculator.component.ts', null, '/app/power-boost-calculator.component.ts')
2015-11-02 03:28:38 -05:00
figure.image-display
2015-12-13 01:01:46 -05:00
img(src='/resources/images/devguide/pipes/power-boost-calculator-anim.gif' alt="Power Boost Calculator")
2015-11-10 13:31:46 -05:00
:marked
2015-10-19 12:56:24 -04:00
2015-10-17 13:01:41 -04:00
.l-main-section
2015-11-10 13:31:46 -05:00
:marked
2015-10-19 12:56:24 -04:00
## Stateful Pipes
2015-11-10 13:31:46 -05:00
There are two categories of pipes, stateless and stateful.
Stateless pipes are pure functions that flow input data
2015-10-19 12:56:24 -04:00
through without remembering anything or causing detectable side-effects.
2015-11-10 13:31:46 -05:00
2015-10-19 12:56:24 -04:00
Most pipes are stateless. The `DatePipe` in our first example is a stateless pipe. So is our custom `ExponentialStrengthPipe`.
2015-11-10 13:31:46 -05:00
Stateful pipes are conceptually similar to classes in object-oriented programming. They can manage the data they transform. A pipe that creates an HTTP request, stores the response and displays the output, is a stateful pipe.
Pipes that retrieve or request data should be used cautiously, since working with network data tends to introduce error conditions that are better handled in JavaScript than in a template.
2015-10-23 12:31:51 -04:00
We can mitigate this risk by creating a custom pipe for a particular backend and bake-in the essential error-handling.
2015-10-19 12:56:24 -04:00
2015-12-01 06:15:14 -05:00
<a id="async-pipe"></a>
2015-10-19 12:56:24 -04:00
## The stateful `AsyncPipe`
2015-11-10 13:31:46 -05:00
The Angular Async pipe is a remarkable example of a stateful pipe.
The Async pipe can receive a Promise or Observable as input
and subscribe to the input automatically, eventually returning the emitted value(s).
2015-10-19 12:56:24 -04:00
It is stateful because the pipe maintains a subscription to the input and its returned values depend on that subscription.
2015-10-23 12:31:51 -04:00
In the next example, we bind a simple promise to a view with the async pipe.
2015-12-13 01:01:46 -05:00
+makeExample('pipes/ts/app/hero-async-message.component.ts', null, 'app/hero-async-message.component.ts')
2015-10-19 12:56:24 -04:00
2015-11-10 13:31:46 -05:00
:marked
2015-10-19 12:56:24 -04:00
The Async pipe saves boilerplate in the component code.
The component doesn't have to subscribe to the async data source,
2015-10-23 12:31:51 -04:00
it doesn't extract the resolved values and expose them for binding,
2016-01-05 15:36:29 -05:00
and (in the case of Observable stream sources like `EventEmitter`)
2015-10-19 12:56:24 -04:00
the component doesn't have to unsubscribe when it is destroyed
(a potent source of memory leaks).
2015-11-10 13:31:46 -05:00
2015-10-19 12:56:24 -04:00
### Implementing a Stateful Pipe
2015-11-10 13:31:46 -05:00
Pipes are stateless by default.
We must declare a pipe to be stateful
2015-11-02 03:28:38 -05:00
by setting the `pure` property of the `@Pipe` decorator to `false`.
2015-11-10 13:31:46 -05:00
This setting tells Angular’ s change detection system to
2015-10-19 12:56:24 -04:00
check the output of this pipe each cycle, whether its input has changed or not.
2015-11-10 13:31:46 -05:00
Here's how we'll decorate our new stateful `FetchJsonPipe` that
2015-10-19 12:56:24 -04:00
makes an HTTP `fetch` request and (eventually) displays the data in the server's response:
2015-12-13 01:01:46 -05:00
+makeExample('pipes/ts/app/fetch-json.pipe.ts', 'pipe-metadata','app/fetch-json.pipe.ts (metadata)')
2015-11-10 13:31:46 -05:00
:marked
Immediately below we have the finished pipe. Its input value is an url to an endpoint that returns a JSON file.
2015-10-19 12:56:24 -04:00
The pipe makes a one-time async request to the server and eventually receives the JSON response.
2015-12-13 01:01:46 -05:00
+makeExample('pipes/ts/app/fetch-json.pipe.ts', null, 'app/fetch-json.pipe.ts')
2015-11-10 13:31:46 -05:00
:marked
2015-12-13 01:01:46 -05:00
Next we demonstrate this pipe in a test component whose template defines two bindings
+makeExample('pipes/ts/app/hero-list.component.ts', 'template', 'app/hero-list.component.ts (template)')
:marked
The component renders like this:
2015-11-02 03:28:38 -05:00
figure.image-display
img(src='/resources/images/devguide/pipes/hero-list.png' alt="Hero List")
2015-12-13 01:01:46 -05:00
:marked
The first binding is straight forward. An `ngFor` repeater displays the hero names fetched from a json source file.
We're piping the literal file name, "heroes.json", through to the custom `fetch` pipe.
### JsonPipe
The second binding uses more pipe chaining.
We take the same fetched results and display the raw hero data in JSON format
by piping to the built-in `JsonPipe`.
.callout.is-helpful
header Debugging with the json pipe
:marked
The [JsonPipe](https://angular.io/docs/ts/latest/api/common/JsonPipe-class.html)
is an easy way to diagnosis a mysteriously failing data binding.
:marked
Here's the complete component implementation:
+makeExample('pipes/ts/app/hero-list.component.ts', null, 'app/hero-list.component.ts')
:marked
2015-10-23 12:31:51 -04:00
.l-main-section
2015-11-10 13:31:46 -05:00
:marked
2015-10-23 12:31:51 -04:00
## Next Steps
2015-11-10 13:31:46 -05:00
2015-10-23 12:31:51 -04:00
Pipes are a great way to encapsulate and share common display-value
transformations. We use them like styles, dropping them
into our templates expressions to enrich the appeal and usability
of our views.
2015-11-10 13:31:46 -05:00
Explore Angular's inventory of built-in pipes in the [API Reference](../api/).
2015-11-17 13:17:14 -05:00
Try writing a custom pipe and perhaps contributing it to the community.