Created
May 12, 2016 08:10
-
-
Save jhades/fe56d88acb61abdddaec9e6d40508e66 to your computer and use it in GitHub Desktop.
Guided Tour To Angular 2 Pipes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component({ | |
selector: 'app', | |
pipes: [SortPipe], | |
template: ` | |
<div class="pipe-example"> | |
<label>Uppercase Pipe: {{ message | uppercase }}</label> | |
</div> | |
<div class="pipe-example"> | |
<label>Lowercase Pipe: {{ message | lowercase }}</label> | |
</div> | |
<div class="pipe-example"> | |
<label>Slice Pipe: {{ message | slice:0:5 }}</label> | |
</div> | |
<div class="pipe-example"> | |
<label>Replace Pipe: {{ message | replace:'World':'Angular 2 World' }}</label> | |
</div> | |
<div class="pipe-example"> | |
<label>Date Pipe: {{ date | date:'yyyy-MMM-dd' }}</label> | |
</div> | |
<div class="pipe-example"> | |
<label>Number Formatting: {{ pi | number:'5.1-2' }}</label> | |
</div> | |
<div class="pipe-example"> | |
<label>Percent Pipe: {{ percentage | percent:'2.1-2' }}</label> | |
</div> | |
<div class="pipe-example"> | |
<label>Currency Pipe: {{ amount | currency:'USD':true:'2.1-2' }}</label> | |
</div> | |
<div class="pipe-example"> | |
<label>Custom Pipe: {{ data | sort:'DESC'}}</label> | |
</div> | |
` | |
}) | |
export class App { | |
message = 'Hello World !'; | |
date = new Date(); | |
pi = 3.14159265359; | |
percentage = 0.0234; | |
amount = 12.1234; | |
data = ['A', 'B', 'H', 'C']; | |
} | |
bootstrap(App); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Pipe({ | |
name: 'sort' | |
}) | |
export class SortPipe implements PipeTransform { | |
transform(array: any[], args): any[] { | |
let sorted = array.sort(); | |
if (args.length > 0 && args === 'DESC' ) { | |
sorted = sorted.reverse(); | |
} | |
return sorted; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment