2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								block includes
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  include ../_util-fns
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 09:56:24 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Every application starts out with what seems like a simple task: get data, transform them, and show them to users.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Getting data could be as simple as creating a local variable or as complex as streaming data over a Websocket.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Once data arrive, we could push their raw `toString` values directly to the view.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  That rarely makes for a good user experience.
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  E.g., almost everyone prefers a simple birthday date like
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  <samp>April 15, 1988</samp> to the original raw string format
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  — <samp>Fri Apr 15 1988 00:00:00 GMT-0700 (Pacific Daylight Time)</samp>.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 09:56:24 -07: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 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  We almost think of them as styles.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 09:56:24 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  In fact, we'd like to apply them in our HTML templates as we do styles.
							 
						 
					
						
							
								
									
										
										
										
											2016-07-03 17:11:17 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-06-06 18:10:37 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Introducing Angular pipes, a way to write display-value transformations that we can declare in our HTML!
							 
						 
					
						
							
								
									
										
										
										
											2016-07-03 17:11:17 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Try the <live-example></live-example>.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 09:56:24 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								.l-main-section
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 09:56:24 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  ## Using Pipes
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00: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
							 
						 
					
						
							
								
									
										
										
										
											2016-06-06 18:10:37 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  a human-friendly date.
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								+makeExample('pipes/ts/app/hero-birthday1.component.ts', null, 'app/hero-birthday1.component.ts')(format='.')
							 
						 
					
						
							
								
									
										
										
										
											2015-11-02 00:28:38 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Focus on the component's template.
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-12 22:01:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								+makeExample('pipes/ts/app/app.component.html', 'hero-birthday-template')(format=".")
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-12-12 22:01:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Inside the interpolation expression we flow the component's `birthday` value through the
							 
						 
					
						
							
								
									
										
										
										
											2016-07-06 14:39:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  [pipe operator](./template-syntax.html#pipe) ( | ) to the [Date pipe](../api/common/index/DatePipe-class.html)
							 
						 
					
						
							
								
									
										
										
										
											2015-12-12 22:01:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  function on the right. All pipes work this way.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 09:56:24 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-07-29 13:08:39 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								.l-sub-section
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  :marked
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    The `Date` and `Currency` pipes need the **ECMAScript Internationalization API**.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    Safari and other older browsers don't support it. We can add support with a polyfill.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  code-example(language="html").
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=Intl.~locale.en"></script>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 09:56:24 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								.l-main-section
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 09:56:24 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  ## Built-in pipes
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Angular comes with a stock of pipes such as
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 09:56:24 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  `DatePipe`, `UpperCasePipe`, `LowerCasePipe`, `CurrencyPipe`, and `PercentPipe`.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  They are all immediately available for use in any template.
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 09:56:24 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								.l-sub-section
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  :marked
							 
						 
					
						
							
								
									
										
										
										
											2016-07-29 13:08:39 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    Learn more about these and many other built-in pipes in the [API Reference](../api/#!?apiFilter=pipe);
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    filter for entries that include the word "pipe".
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    Angular 2 doesn't have a `FilterPipe` or an `OrderByPipe` for reasons explained in an [appendix below](#no-filter-pipe).
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 09:56:24 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								.l-main-section
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 09:56:24 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  ## Parameterizing a Pipe
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  A pipe may accept any number of optional parameters to fine-tune its output.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 09:56:24 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  We add parameters to a pipe by following the pipe name with a colon ( : ) and then the parameter value
							 
						 
					
						
							
								
									
										
										
										
											2015-10-23 09:31:51 -07: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 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-12 22:01:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  We'll modify our birthday template to give the date pipe a format parameter.
							 
						 
					
						
							
								
									
										
										
										
											2016-06-06 18:10:37 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  After formatting the hero's April 15th birthday, it should render as **<samp>04/15/88</samp>**:
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 09:56:24 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-12 22:01:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								+makeExample('pipes/ts/app/app.component.html', 'format-birthday')(format=".")
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 09:56:24 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 09:56:24 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  The parameter value can be any valid
							 
						 
					
						
							
								
									
										
										
										
											2016-02-12 20:24:42 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  [template expression](./template-syntax.html#template-expressions)
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  such as a string literal or a component property.
							 
						 
					
						
							
								
									
										
										
										
											2015-12-12 22:01:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  In other words, we can control the format through a binding the same way we control the birthday value through a binding.
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-12 22:01:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  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:
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-12 22:01:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								+makeExample('pipes/ts/app/hero-birthday2.component.ts', 'template', 'app/hero-birthday2.component.ts (template)')(format=".")
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  We also added a button to the template and bound its click event to the component's `toggleFormat()` method.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 09:56:24 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  That method toggles the component's `format` property between a short form
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  (`'shortDate'`) and a longer form (`'fullDate'`).
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								+makeExample('pipes/ts/app/hero-birthday2.component.ts', 'class', 'app/hero-birthday2.component.ts (class)')(format='.')
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-12 22:01:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 09:56:24 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  As we click the button, the displayed date alternates between
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  "**<samp>04/15/1988</samp>**" and
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  "**<samp>Friday, April 15, 1988</samp>**".
							 
						 
					
						
							
								
									
										
										
										
											2015-11-02 00:28:38 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								figure.image-display
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    img(src='/resources/images/devguide/pipes/date-format-toggle-anim.gif' alt="Date Format Toggle")
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-12-12 22:01:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 09:56:24 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								.l-sub-section
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  :marked
							 
						 
					
						
							
								
									
										
										
										
											2016-07-06 14:39:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    Learn more about the `DatePipes` format options in the [API Docs](../api/common/index/DatePipe-class.html).
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 09:56:24 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  ## Chaining pipes
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  We can chain pipes together in potentially useful combinations.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 09:56:24 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  In the following example, we chain the birthday to the `DatePipe` and on to the `UpperCasePipe`
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  so we can display the birthday in uppercase. The following birthday displays as
							 
						 
					
						
							
								
									
										
										
										
											2016-06-06 18:10:37 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  **<samp>APR 15, 1988</samp>**.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-02 00:28:38 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-12 22:01:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								+makeExample('pipes/ts/app/app.component.html', 'chained-birthday')(format=".")
							 
						 
					
						
							
								
									
										
										
										
											2015-11-02 00:28:38 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  This example — which displays **<samp>FRIDAY, APRIL 15, 1988</samp>** —
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  chains the same pipes as above, but passes in a parameter to `date` as well.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-02 00:28:38 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-12 22:01:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								+makeExample('pipes/ts/app/app.component.html', 'chained-parameter-birthday')(format=".")
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 09:56:24 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								.l-main-section
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 09:56:24 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  ## Custom Pipes
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-02 00:28:38 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  We can write our own custom pipes.
							 
						 
					
						
							
								
									
										
										
										
											2015-12-12 22:01:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Here's a custom pipe named `ExponentialStrengthPipe` that can boost a hero's powers:
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 09:56:24 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								+makeExample('pipes/ts/app/exponential-strength.pipe.ts', null, 'app/exponential-strength.pipe.ts')(format=".")
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2016-06-06 18:10:37 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  This pipe definition reveals several key points:
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-12 22:01:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  * A pipe is a class decorated with pipe metadata.
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-28 11:52:41 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  * The pipe class implements the `PipeTransform` interface's `transform` method that
							 
						 
					
						
							
								
									
										
										
										
											2016-04-27 16:45:21 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  accepts an input value followed by optional parameters and returns the transformed value.
							 
						 
					
						
							
								
									
										
										
										
											2015-12-12 22:01:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-04-27 16:45:21 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  * There will be one additional argument to the `transform` method for each parameter passed to the pipe.
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Our pipe has one such parameter: the `exponent`.
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-12 22:01:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  * We tell Angular that this is a pipe by applying the
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  `@Pipe` #{_decorator} which we import from the core Angular library.
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  * The `@Pipe` #{_decorator} allows us to define the
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								     pipe name that we'll use within template expressions. It must be a valid JavaScript identifier.
							 
						 
					
						
							
								
									
										
										
										
											2016-01-15 14:30:00 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								     Our pipe's name is `exponentialStrength`.
							 
						 
					
						
							
								
									
										
										
										
											2015-12-12 22:01:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-28 11:52:41 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								.l-sub-section
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  :marked
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    ### The *PipeTransform* Interface
							 
						 
					
						
							
								
									
										
										
										
											2015-12-12 22:01:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    The `transform` method is essential to a pipe.
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    The `PipeTransform` *interface* defines that method and guides both tooling and the compiler.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    It is technically optional; Angular looks for and executes the `transform` method regardless.
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-28 11:52:41 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-12-12 22:01:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Now we need a component to demonstrate our pipe.
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								+makeExample('pipes/ts/app/power-booster.component.ts',null,'app/power-booster.component.ts')(format='.')
							 
						 
					
						
							
								
									
										
										
										
											2015-11-02 00:28:38 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								figure.image-display
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    img(src='/resources/images/devguide/pipes/power-booster.png' alt="Power Booster")
							 
						 
					
						
							
								
									
										
										
										
											2015-10-17 10:01:41 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-10-23 09:31:51 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Two things to note:
							 
						 
					
						
							
								
									
										
										
										
											2015-12-12 22:01:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  1. We use our custom pipe the same way we use the built-in pipes.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-08-09 17:38:25 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  1. We must include our pipe in the `declarations` #{_array} of the AppModule.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-12 22:01:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								.callout.is-helpful
							 
						 
					
						
							
								
									
										
										
										
											2016-08-09 17:38:25 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  header Remember the declarations #{_array}!
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  :marked
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    Angular reports an error if we neglect to list our custom pipe.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 09:56:24 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    We didn't list the `DatePipe` in our previous example because all
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    Angular built-in pipes are pre-registered.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 09:56:24 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    Custom pipes must be registered manually.
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-07-03 17:11:17 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  If we try the <live-example></live-example>,
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 09:56:24 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  we can probe its behavior by changing the value and the optional exponent in the template.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 09:56:24 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  ## Power Boost Calculator (extra-credit)
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 09:56:24 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  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-12 22:01:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  our pipe and two-way data binding with `ngModel`.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 09:56:24 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								+makeExample('pipes/ts/app/power-boost-calculator.component.ts', null, '/app/power-boost-calculator.component.ts')(format='.')
							 
						 
					
						
							
								
									
										
										
										
											2015-11-02 00:28:38 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								figure.image-display
							 
						 
					
						
							
								
									
										
										
										
											2015-12-12 22:01:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    img(src='/resources/images/devguide/pipes/power-boost-calculator-anim.gif' alt="Power Boost Calculator")
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 09:56:24 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-17 10:01:41 -07:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								.l-main-section
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								a#change-detection
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  ## Pipes and Change Detection
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Angular looks for changes to data-bound values through a *change detection* process that runs after every JavaScript event:
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  every keystroke, mouse move, timer tick, and server response. This could be expensive.
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Angular strives to lower the cost whenever possible and appropriate.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Angular picks a simpler, faster change detection algorithm when we use a pipe. Let's see how.
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  ### No pipe
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  The component in our next example uses the default, aggressive change detection strategy to monitor and update
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  its display of every hero in the `heroes` #{_array}. Here's the template:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								+makeExample('pipes/ts/app/flying-heroes.component.html', 'template-1', 'app/flying-heroes.component.html (v1)')(format='.')
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  The companion component class provides heroes, adds new heroes into the #{_array}, and can reset the #{_array}.
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								+makeExample('pipes/ts/app/flying-heroes.component.ts', 'v1', 'app/flying-heroes.component.ts (v1)')(format='.')
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  We can add a new hero and Angular updates the display when we do.
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  The `reset` button replaces `heroes` with a new #{_array} of the original heroes and Angular updates the display when we do.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  If we added the ability to remove or change a hero, Angular would detect those changes too and update the display as well.
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  ### Flying Heroes pipe
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Let's add a `FlyingHeroesPipe` to the `*ngFor` repeater that filters the list of heroes to just those heroes who can fly.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+makeExample('pipes/ts/app/flying-heroes.component.html', 'template-flying-heroes', 'app/flying-heroes.component.html (flyers)')(format='.')
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Here's the `FlyingHeroesPipe` implementation which follows the pattern for custom pipes we saw earlier.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+makeExample('pipes/ts/app/flying-heroes.pipe.ts', 'pure', 'app/flying-heroes.pipe.ts')(format='.')
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 09:56:24 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-05-20 16:18:58 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2016-07-03 17:11:17 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  When we run the sample now we see odd behavior (try it in the <live-example></live-example>).
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  Every hero we add is a flying hero but none of them are displayed.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Although we're not getting the behavior we want, Angular isn't broken.
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  It's just using a different change detection algorithm — one that ignores changes to the list or any of its items.
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Look at how we're adding a new hero:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+makeExample('pipes/ts/app/flying-heroes.component.ts', 'push')(format='.')
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  We're adding the new hero into the `heroes` #{_array}.  The reference to the #{_array} hasn't changed.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  It's the same #{_array}. That's all Angular cares about. From its perspective, *same #{_array}, no change, no display update*.
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  We can fix that. Let's create a new #{_array} with the new hero appended and assign that to `heroes`.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  This time Angular detects that the #{_array} reference has changed.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  It executes the pipe and updates the display with the new #{_array} which includes the new flying hero.
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  *If we **mutate** the #{_array}, no pipe is invoked and no display updated;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  if we **replace** the #{_array}, then the pipe executes and the display is updated*.
							 
						 
					
						
							
								
									
										
										
										
											2016-05-20 16:18:58 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  The *Flying Heroes* extends the
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  code with checkbox switches and additional displays to help us experience these effects.
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								figure.image-display
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    img(src='/resources/images/devguide/pipes/flying-heroes-anim.gif' alt="Flying Heroes")
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Replacing the #{_array} is an efficient way to signal to Angular that it should update the display.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  When do we replace the #{_array}? When the data change.
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  That's an easy rule to follow in *this toy* example
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  where the only way to change the data is by adding a new hero.
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  More often we don't know when the data have changed,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  especially in applications that mutate data in many ways,
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  perhaps in application locations far away.
							 
						 
					
						
							
								
									
										
										
										
											2016-06-29 17:46:01 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  A component in such an application usually can't know about those changes.
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Moreover, it's unwise to distort our component design to accommodate a pipe.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  We strive as much as possible to keep the component class independent of the HTML.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  The component should be unaware of pipes.
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Perhaps we should consider a different kind of pipe for filtering flying heroes, an *impure pipe*.
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								.l-main-section
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  ## Pure and Impure Pipes
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  There are two categories of pipes: **pure** and **impure**.
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Pipes are pure by default. Every pipe we've seen so far has been pure.
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  We make a pipe impure by setting its pure flag to false. We could make the `FlyingHeroesPipe`
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  impure like this:
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								+makeExample('pipes/ts/app/flying-heroes.pipe.ts', 'pipe-decorator')(format='.')
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  Before we do that, let's understand the difference between *pure* and *impure*, starting with a *pure* pipe.
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  ### Pure pipes
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								block pure-change
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  :marked
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    Angular executes a *pure pipe* only when it detects a *pure change* to the input value.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    A ***pure change*** is *either* a change to a primitive input value (`String`, `Number`, `Boolean`, `Symbol`)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    *or* a changed object reference (`Date`, `Array`, `Function`, `Object`).
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  Angular ignores changes *within* (composite) objects.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  It won't call a pure pipe if we change an input month, add to an input #{_array}, or update an input object property.
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  This may seem restrictive but is is also fast.
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  An object reference check is fast — much faster than a deep check for
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  differences — so Angular can quickly determine if it can skip both the
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  pipe execution and a view update.
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  For this reason, we prefer a pure pipe if we can live with the change detection strategy.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  When we can't, we *may* turn to the impure pipe.
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								.l-sub-section
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  :marked
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    Or we might not use a pipe at all.
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    It may be better to pursue the pipe's purpose with a property of the component,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    a point we take up later.
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  ### Impure pipes
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Angular executes an *impure pipe*  during *every* component change detection cycle.
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  An impure pipe will be called a lot, as often as every keystroke or mouse-move.
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  With that concern in mind, we must implement an impure pipe with great care.
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  An expensive, long-running pipe could destroy the user experience.
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  <a id="impure-flying-heroes"></a>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  ### An impure *FlyingHeroesPipe*
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  A flip of the switch turns our `FlyingHeroesPipe` into a `FlyingHeroesImpurePipe`.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  Here's the complete implementation:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+makeTabs(
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  'pipes/ts/app/flying-heroes.pipe.ts, pipes/ts/app/flying-heroes.pipe.ts',
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  'impure, pure',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  'FlyingHeroesImpurePipe, FlyingHeroesPipe')(format='.')
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  We inherit from `FlyingHeroesPipe` to prove the point that nothing changed internally.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  The only difference is the `pure` flag in the pipe metadata.
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  This is a good candidate for an impure pipe because the `transform` function is trivial and fast.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+makeExample('pipes/ts/app/flying-heroes.pipe.ts','filter')(format='.')
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 09:56:24 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  We can derive a `FlyingHeroesImpureComponent` that we derive from the `FlyingHeroesComponent`.
							 
						 
					
						
							
								
									
										
										
										
											2016-08-09 17:38:25 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								+makeExample('pipes/ts/app/flying-heroes-impure.component.html','template-flying-heroes','app/flying-heroes-impure.component.html (FlyingHeroesImpureComponent)')(format='.')
							 
						 
					
						
							
								
									
										
										
										
											2016-07-03 17:11:17 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2016-08-09 17:38:25 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  The only substantive change is the pipe in the template.
							 
						 
					
						
							
								
									
										
										
										
											2016-07-03 17:11:17 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  We can confirm in the <live-example></live-example> that the _flying heroes_
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  display updates as we enter new heroes even when we mutate the `heroes` #{_array}.
							 
						 
					
						
							
								
									
										
										
										
											2016-05-20 16:18:58 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								- var _dollar = _docsFor === 'ts' ? '$' : '';
							 
						 
					
						
							
								
									
										
										
										
											2016-05-20 16:18:58 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								h3#async-pipe The impure #[i AsyncPipe]
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  The Angular `AsyncPipe` is an interesting example of an impure pipe.
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  The `AsyncPipe` accepts a `#{_Promise}` or `#{_Observable}` as input
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  and subscribes to the input automatically, eventually returning the emitted value(s).
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  It is also stateful.
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  The pipe maintains a subscription to the input `#{_Observable}` and
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  keeps delivering values from that `#{_Observable}` as they arrive.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  In this next example, we bind an `#{_Observable}` of message strings 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  (`message#{_dollar}`) to a view with the `async` pipe.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 09:56:24 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-12 22:01:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								+makeExample('pipes/ts/app/hero-async-message.component.ts', null, 'app/hero-async-message.component.ts')
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 09:56:24 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 09:56:24 -07: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 09:31:51 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  it doesn't extract the resolved values and expose them for binding,
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  and the component doesn't have to unsubscribe when it is destroyed
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 09:56:24 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  (a potent source of memory leaks).
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  ### An impure caching pipe
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Let's write one more impure pipe, a pipe that makes an HTTP request to the server.
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Normally, that's a horrible idea.
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  It's probably a horrible idea no matter what we do.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  We're forging ahead anyway to make a point.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  Remember that impure pipes are called every few microseconds.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  If we're not careful, this pipe will punish the server with requests.
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  We are careful. Our pipe only makes a server call if the request URL has changed.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  It caches the request URL and waits for a result which it also caches when it arrives.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  The pipe returns the cached result (which is null while a request is in flight)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  after every Angular call and only contacts the server as necessary.
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Here's the code, which uses the [Angular http](server-communication.html) facility
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  to retrieve a `heroes.json` file:
							 
						 
					
						
							
								
									
										
										
										
											2015-10-19 09:56:24 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-12 22:01:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								+makeExample('pipes/ts/app/fetch-json.pipe.ts', null, 'app/fetch-json.pipe.ts')
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Then we demonstrate it in a harness component whose template defines two bindings to this pipe.
							 
						 
					
						
							
								
									
										
										
										
											2015-12-12 22:01:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								+makeExample('pipes/ts/app/hero-list.component.ts', 'template', 'app/hero-list.component.ts (template)')
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Despite the two bindings and what we know to be frequent pipe calls,
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  the nework tab in the browser developer tools confirms that there is only one request for the file.
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-12 22:01:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  The component renders like this:
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-11-02 00:28:38 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								figure.image-display
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    img(src='/resources/images/devguide/pipes/hero-list.png' alt="Hero List")
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-12 22:01:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  ### *JsonPipe*
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  The second binding involving the `FetchPipe` uses more pipe chaining.
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  We take the same fetched results displayed in the first binding
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  and display them again, this time in JSON format by chaining through to the built-in `JsonPipe`.
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-12 22:01:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								.callout.is-helpful
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  header Debugging with the json pipe
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  :marked
							 
						 
					
						
							
								
									
										
										
										
											2016-07-06 14:39:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    The [JsonPipe](../api/common/index/JsonPipe-class.html)
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    provides an easy way to diagnosis a mysteriously failing data binding or
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    inspect an object for future binding.
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-12 22:01:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  Here's the complete component implementation:
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-12-12 22:01:46 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								+makeExample('pipes/ts/app/hero-list.component.ts', null, 'app/hero-list.component.ts')
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								a(id="pure-pipe-pure-fn")
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2016-04-21 01:12:21 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  ### Pure pipes and pure functions
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-04-21 01:12:21 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  A pure pipe uses pure functions.
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Pure functions process inputs and return values without detectable side-effects.
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Given the same input they should always return the same output.
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  The pipes we saw earlier in this chapter were implemented with pure functions.
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  The built-in `DatePipe` is a pure pipe with a pure function implementation.
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  So is our `ExponentialStrengthPipe`.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  So is our `FlyingHeroesPipe`.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  A few steps back we reviewed the `FlyingHeroesImpurePipe` — *an impure pipe with a pure function*.
							 
						 
					
						
							
								
									
										
										
										
											2016-04-21 01:12:21 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  But a *pure pipe* must always be implemented with a *pure function*. Failure to heed this warning will bring about many a console errors regarding expressions that have changed after they were checked.
							 
						 
					
						
							
								
									
										
										
										
											2015-10-23 09:31:51 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								.l-main-section
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
									
										
										
										
											2015-10-23 09:31:51 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  ## Next Steps
							 
						 
					
						
							
								
									
										
										
										
											2015-11-10 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-10-23 09:31:51 -07: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 18:31:46 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Explore Angular's inventory of built-in pipes in the [API Reference](../api/#!?apiFilter=pipe).
							 
						 
					
						
							
								
									
										
										
										
											2015-11-17 13:17:14 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Try writing a custom pipe and perhaps contributing it to the community.
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								a(id="no-filter-pipe")
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								.l-main-section
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  ## No *FilterPipe* or *OrderByPipe*
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Angular does not ship with pipes for filtering or sorting lists.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  Developers familiar with Angular 1 know these as `filter` and `orderBy`.
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  There are no equivalents in Angular 2.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  This is not an oversight. Angular 2 is unlikely to offer such pipes because
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  (a) they perform poorly and (b) they prevent aggressive minification.
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Both `filter` and `orderBy` require parameters that reference object properties.
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  We learned earlier that such pipes must be  [*impure*](#pure-and-impure-pipes) and that
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  Angular calls impure pipes in almost every change detection cycle.
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  Filtering and especially sorting are expensive operations.
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  The user experience can degrade severely for even moderate sized lists when Angular calls these pipe methods many times per second.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  The `filter` and `orderBy` have often been abused in Angular 1 apps, leading to complaints that Angular itself is slow.
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  That charge is fair in the indirect sense that Angular 1 prepared this performance trap
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  by offering `filter` and `orderBy` in the first place.
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  The minification hazard is also compelling if less obvious. Imagine a sorting pipe applied to a list of heroes.
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 13:44:14 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  We might sort the list by hero `name` and `planet` of origin properties something like this:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								code-example(language="html")
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  <!-- NOT REAL CODE! -->
							 
						 
					
						
							
								
									
										
										
										
											2016-05-02 00:29:25 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  <div *ngFor="let hero of heroes | orderBy:'name,planet'"></div>
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								:marked
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  We identify the sort fields by text strings, expecting the pipe to reference a property value by indexing
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  (e.g., `hero['name']`).
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  Unfortunately, aggressive minification *munges* the `Hero` property names so that `Hero.name` and `Hero.planet`
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  becomes something like `Hero.a` and `Hero.b`. Clearly `hero['name']` is not going to work.
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  Some of us may not care to minify this aggressively. That's *our* choice.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  But the Angular product should not prevent someone else from minifying aggressively.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  Therefore, the Angular team decided that everything shipped in Angular will minify safely.
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  The Angular team and many experienced Angular developers strongly recommend that you move
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  filtering and sorting logic into the component itself.
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  The component can expose a `filteredHeroes` or `sortedHeroes` property and take control
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  over when and how often to execute the supporting logic.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  Any capabilities that you would have put in a pipe and shared across the app can be
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  written in a filtering/sorting service and injected into the component.
							 
						 
					
						
							
								
									
										
										
										
											2016-04-06 21:15:07 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  If these performance and minification considerations do not apply to you, you can always create your own such pipes
							 
						 
					
						
							
								
									
										
										
										
											2016-01-13 15:00:43 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  (along the lines of the [FlyingHeroesPipe](#impure-flying-heroes)) or find them in the community.