Custom Pipes
Angular allows you to create your own custom pipes as well. Below is an example of custom pipe. It allows you to pass file sizes as input in bytes and displays output in more redable form. eg. 1024
bytes as input displays 1 KB
as output in short form and 1 Kilobytes
in long form.
Each custom pipe implementation must:
have the
@Pipe
decorator with pipe metadata that has aname
property. This value will be used to call this pipe in template expressions. It must be a valid JavaScript identifier.implement the
PipeTransform
interface's transform method. This method takes the value being piped and a variable number of arguments of any type and return a transformed ("piped") value.
Each colon-delimited parameter in the template maps to one method argument in the same order.
Above pipe can be used in below two ways:
Short Form:
{{ largeFileSize | formatFileSize }}
displaying output as1 KB
Long Form :
{{ largeFileSize | formatFileSize:true }}
displaying output as1 Kilobytes
Last updated