Jim Lynch Codes
  • Blog
  • About Jim

Writings about one coder's stories & experiences.

Simplest Ever Custom Pipe In Angular 2

1/5/2017

0 Comments

 
This is a post meant to help you understand pipes in Angular 2, and what better place to start than the simplest pipe possible? :)

From Filters To Pipes

If you're familiar with AngularJS then you might be wondering why there are no filters in Angular 2. Well, you can pretty much think of pipes as the Angular 2 equivalent for filters. Although creating each uses a different syntax, they provide the same purpose; to transform data in the view while preserving the data in it's original form. For example, the currency filter allows you to work with pure numbers that can be combined with ordinary mathematical operations while at the same time displaying the data in a nice format with a currency symbol and at most only 2 digits past the decimal.

Angular CLI Pipe Scaffolding

If you're using the Angular CLI you can scaffold out a stub for a pipe super easily with the generate command (shorthand is just g):
ng g pipe derpy-derp
This will generate a file named derpy-derp.pipe.ts (and also a unit test file named derp-derp.spec.ts) in the app/ directory. You could also prefix the name "derpy-derp" with folder names and slashes in order to generate your files further down in your directories. This command will generate some boilerplate code for a pipe that should look something like this:
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'derpyDerp'
})
export class DerpyDerpPipe implements PipeTransform {

  transform(value: any, args?: any): any {
    return null;
  }

}
The above code is what's generated by the Angular CLI, but a pipe that just returns null is no fun. Let's instead have it return something simple, like the number 42!
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'derpyDerp'
})
export class DerpyDerpPipe implements PipeTransform {

  transform(value: any, args?: any): any {
    return 42;
  }

}

Using A Pipe In Your Template

In the HTML markup we can make use of this pipe. Normally we're going to want to transform some dynamic piece of data that is represented by a variable in our component. Inside our our Angular 2 data-binding double curlies we can add the pipe symbol followed by our pipe's name:
<div>
  {{ points | derpyDerp }}
<div>
We can also pass arguments to our pipe like so:
<div>
  {{ points | derpyDerp: arg1:arg2:arg3... }}
<div>
And then we can reference these arguments in our pipe's transform method like this:
  transform(value:any, arg1:any, arg2:any, arg3:any):any

Using Your Pipe In The TypeScript

Although you will often see pipes being used in the HTML with the pipe symbol, you can also make use of your pipes directly from the TypeScript code. Since our pipe is just a TypeScript class we can treat it like a regular old class. That means if we want to use our pipe to transform some data all we need to do is instantiate the pipe and call it's trandform method! Wow!
new DerpyDerpPipe().transform(myData, arg1, arg2, arg3) 
0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    ​Author

    Picture
    The posts on this site are written and maintained by Jim Lynch. About Jim...
    Picture
    Follow @JimLynchCodes
    Follow @JimLynchCodes

    Categories

    All
    Actionscript 3
    Angular
    AngularJS
    Automated Testing
    AWS Lambda
    Behavior Driven Development
    Blockchain
    Blogging
    Business Building
    C#
    C / C++
    ClojureScript / Clojure
    Coding
    Community Service
    CS Philosophy
    Css / Scss
    Dev Ops
    Firebase
    Fitness
    Flash
    Front End
    Functional Programming
    Git
    Go Lang
    Haskell
    Illustrations
    Investing
    Java
    Javascript
    Lean
    Life
    Linux
    Logic Pro
    Music
    Node.js
    Planning
    Productivity
    Professionalism
    Python
    React
    Redux / Ngrx
    Refactoring
    Reusable Components
    Rust
    Security
    Serverless
    Shell Scripting
    Swift
    Test Driven Development
    Things
    TypeScript
    Useful Sites
    Useful Tools
    Video
    Website Development
    WebStorm
    Writing

    Archives

    March 2023
    August 2021
    February 2021
    January 2021
    October 2020
    September 2020
    May 2020
    April 2020
    February 2020
    January 2020
    December 2019
    October 2019
    September 2019
    August 2019
    July 2019
    June 2019
    May 2019
    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    June 2018
    May 2018
    April 2018
    March 2018
    February 2018
    January 2018
    December 2017
    November 2017
    October 2017
    September 2017
    August 2017
    July 2017
    May 2017
    April 2017
    March 2017
    February 2017
    January 2017
    December 2016
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    January 2016
    December 2015
    November 2015
    October 2015

    RSS Feed

  • Blog
  • About Jim
JimLynchCodes © 2023