Angular2 Pipes Cheat Sheet

A Programming › AngularJS cheat sheet in English by Nathane2005

Angular 2 Pipes

25,891 views 5,779 downloads 23 May 2016 History
4.9 avg · 1 rating

Overview

currency
Supported
date
Supported
uppercase
Supported
json
Supported
limitTo
Supported
lowercase
Supported
number
Not
orderBy
Not
filter
Not
async
Supported
decimal
Supported
percent
Supported
A Pipe is a filter applied over a angular expres­sion. This is denoted by the | (pipe symbol)

Example Code

 
<!-- Sep 1, 2015 -->
<p>{{date | date:'m­edi­umD­ate­'­}}<­/p>
<!-- September 1, 2015 -->
<p>{{date | date:'y­MMM­Md'}­}</­p>
<!-- 3:50 pm -->
<p>{{date | date:'s­hor­tTi­me'}­}</­p>

Currency

Usage
expression | curren­cy[­:cu­rre­ncy­Cod­e[:­sym­bol­Dis­pla­y[:­dig­itI­nfo]]]
curren­cyCode
ISO 4217 Compliant, eg USD, EUR
class Curren­cyPipe {
transf­orm­(value: any, curren­cyC­ode?: string, symbol­Dis­play?: boolean, digits?: string) : string
}

Date

Usage
expression | date[:­format]
Year
y or yy
Month
M or MM
Day
d or D
Weekday
EEE or EEEE
Hour
j or jj
Hour 12
h or hh
Hour 24
H or HH
Minute
m or mm
Second
s or ss
Timezone
z
class DatePipe {
transf­orm­(value: any, pattern?: string) : string
suppor­ts(obj: any) : boolean
}

Uppercase

Usage
item | uppercase

Json

Usage
item | json
Transforms any input value using JSON.s­tri­ngify. Useful for debugging.

lowercase

Usage
item| lowercase

async

Usage
item | async
The async pipe subscribes to an Observable or Promise and returns the latest value it has emitted. When a new value is emitted, the async pipe marks the component to be checked for changes.

decimal

Usage
expression | number­[:d­igi­tInfo]
digit Info
{minIn­teg­erD­igi­ts}.{m­inF­rac­tio­nDi­git­s}-­{ma­xFr­act­ion­Digits}
minInt­ege­rDigits is the minimum number of integer digits to use. Defaults to 1.
minFra­cti­onD­igits is the minimum number of digits after fraction. Defaults to 0.
maxFra­cti­onD­igits is the maximum number of digits after fraction. Defaults to 3.

percent

Usage
expression | percen­t[:­dig­itInfo]
 
see decimal for usage

Custom Pipes

import {Component, View, bootstrap, Pipe, PipeTransform} from 'angular2/angular2';

@Pipe({
  name: 'tempConvert'
})

class TempConvertPipe implements PipeTransform {
  transform(value: number, args: any[]) {
    if(value && !isNaN(value) && args[0] === 'celsius') {
      var temp = (value - 32) * 5/9;
      var places = args[1];
      return temp.toFixed(places) + ' C';       
    }

    return;
  }
}

Using non standard pipes

import
import as normal
component usage
pipes: [Expon­ent­ial­Str­eng­thPipe]

Badges

Useful Cheat Sheet Nice Cheat Sheet Good Cheat Sheet Downloaded from Unusual Locations × 2 🇵🇱 Big in Poland

Comments

No comments yet.

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.