| Start | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Custom Pipes my.pipe.ts my.component.ts my.module.ts |
||||||||||||||||||
Built-in Pipes Angular2 comes with a few built-in pipes:
There are others. Look here (opens new window) for their documentation. Examplehotel-reservation.component.tshotel-reservation.template.htmlOutput |
||||||||||||||||||
| Chaining Pipes Pipes may be chained. |
||||||||||||||||||
| Debugging With JsonPipe The JsonPipe can be used for debugging the state of any given internal. CodeOutput |
||||||||||||||||||
| Dynamic Pipe Use case scenario: A table view consists of different columns with different data format that needs to be transformed with different pipes. table.component.ts dynamic.pipe.ts table.component.html Result |
||||||||||||||||||
Unwrap async values with async pipe Becomes: |
||||||||||||||||||
| Stateful Pipes Angular 2 offers two different types of pipes - stateless and stateful. Pipes are stateless by default. However, we can implement stateful pipes by setting the Important Notice that most pipes should fall into the category of stateless pipes. That's important for performance reasons since Angular can optimize stateless pipes for the change detector. So use stateful pipes cautiously. In general, the optimization of pipes in Angular 2 have a major performance enhancement over filters in Angular 1.x. In Angular 1 the digest cycle always had to re-run all filters even though the data hasn't changed at all. In Angular 2, once a pipe's value has been computed, the change detector knows not to run this pipe again unless the input changes. Implementation of a stateful pipe You can then use the pipe as usual: It's important that your pipe also implements the |
||||||||||||||||||
| Creating Custom Pipe app/pipes.pipe.ts app/my-component.component.ts |
||||||||||||||||||
| Globally Available Custom Pipe To make a custom pipe available application wide, During application bootstrap, extending PLATFORM_PIPES. Tutorial here: https://scotch.io/tutorials/create-a-globally-available-custom-pipe-in-angular-2 (opens new window) |
||||||||||||||||||
Extending an Existing Pipe |
||||||||||||||||||
| Testing a pipe Given a pipe that reverse a string It can be tested configuring the spec file like this Parameters
RemarksThis topic covers Angular2 Pipes (opens new window), a mechanism for transforming and formatting data within HTML templates in an Angular2 application. |
Read Article
https://develop.horje.com/learn/1434/reference