Design a site like this with WordPress.com
Get started

Angular JS Custom PIPE Examples

Prerequisites

Angularj JS new version is using nodejs for setting up application and for running applicaion you will also need any code editor tool.

For current setup we are using visual code studio

Kindly down load latest version of nodejs and visual code studio on your machine and complete setup

Node Installation

1 Go to this site and download latest version of node js

https://nodejs.org/en/

2) Once installed open command prompt and type “node”

you will be able to see version on node js that means nodejs installed

Install Visual Studio

1 Go to this site and download latest version of

https://code.visualstudio.com/

2. Once installed on windows go to search menu and type visual you will be able to see its installed like below screenshot

3.Open Application and you will be able to see below screen

4) Select folder from file menu for your project

5) Open Terminal from Terminal option

Install Angular cli

Run below command

  npm install -g @angular/cli

once its installed type below command in terminal to verify installation

Create a workspace and initial application

Run below command

  ng new my-app

it will ask for angular routing =>select yes

then select css as style sheer language like below screenshot

Press Enter and it will generate your project

in below diagram 1) Creating html and components from schematics 2) DOwnloading required packages you can see under node_modules folder

Run the application

Run below command

traverse to application folder

 cd my-app  

run below command to start and open application

ng serve –open

this command will compile app and launch it with webpack on http://localhost:4200/

Congrats your first application is running and working fine

Creating Hello World

In angular data binding is happening between html and component classes .

Here we will declare one variable in component file and same will be visible on UI .

  1. Delete everything from app.component.html.

2. Declare variable in app.component.ts

app.component.ts

import { Component } from '@angular/core';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'my-app';
  message = 'Hello World';
}

3. Now add binding between UI and component with scriplet tag

app.component.html

<div>
<h1>{{message}}</h1>
</div>

4. Run below command to compile app

ng serve

5. Open browser and enter http://localhost:4200/

Your application is running and rendered “Hello World” in browser. Same way you can initialize variables and pass data from component to UI.

Now we have created hello world application with single component and now we will create additional component which will be useful to add re usability.

Use Custom Pipe in AngularJS

Angularjs has provided facility to use predefined and custom pipes for transformation of data.These can be very useful in transforming data .

Angularjs also given opportunity to create your own pipes.

In this tutorial we will create custom pipe which will append string to existing string

Generate Pipe

ng generate pipe custompipeappend

This will create 2 files in your project directory.

This will also add entry in app.module.ts file so it can be used in project easily

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import { CustompipeappendPipe } from './custompipeappend.pipe';

@NgModule({
  declarations: [
    AppComponent,
    CustompipeappendPipe
  ],
  imports: [
    BrowserModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

custompipeappend.pipe.ts

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'custompipeappend'
})
export class CustompipeappendPipe implements PipeTransform {

  transform(value: any, ...args: any[]): any {
    return value + 'custompipe';
  }

}

Custom pipe are using @Pipe decorator which denotes it as pipe

This is implementatoin of custom pipe where angular js will provide value and arguments in transform method and then you can add logic according to your need.

IN our case we have just appended string custompipe ahead of value which is coming in.

app.component.ts

import { Component } from ‘@angular/core’;

@Component({
selector: ‘app-root’,
templateUrl: ‘./app.component.html’,
styleUrls: [‘./app.component.css’]
})
export class AppComponent {
title = ‘Hellowworld’;
}

In this component we have declared variable title as Hellowworld.

app.component.html

<h1>{{title | custompipeappend}}</h1>

In above example we have used below custom pipes to demonstrate angular pipe. So after custome pipe get applied on title then output will be “Hellowworldcustompipe”

Run below command to compile app

ng serve –open

Open App in browser

use below url to open in browser

http://localhost:4200/

Now you can see custom pipe has been applied on “title” variable and below output has been produced

GIT REPO:

https://github.com/letsblogcontent/AngularJS/tree/master/custompipe

Video Tutorial

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: