In this article, we will learn how to create custom pipe.
Remember that we showed message when viewing the customer’s details by sending an object: SysEvent
with EventEmitter
.
Here we want to show more SysEvent
's properties like this:
2016/10/25 19:33 [Info] You are looking at HACHI’s information!
Since we already have all the properties we need in class: SysEvent
, we just need
to write a custom pipe for showing the formatted message.
Let’s start creating a custom pipe in customer.pipe.ts
.
import { Pipe, PipeTransform } from '@angular/core';
import { DatePipe } from '@angular/common';
import {Customer} from '../../class/Customer';
import {SysEvent} from '../../class/SysEvent';
@Pipe({
name: 'wrapEvent'
})
export class WrapEventPipe implements PipeTransform {
constructor() {}
transform(content: SysEvent) {
//Remove html tags
let msg = content.Msg;
var rex = /(<([^>]+)>)/ig;
msg = msg.replace(rex, "");
//Convert date to string
var datePipe = new DatePipe("en-US");
let createOn = datePipe.transform(content.CreateOn, 'yyyy/MM/dd HH:mm');
let title = content.Title;
let createBy = content.CreateBy;
return createOn.concat(' [', title, '] ', msg);
}
}
In the above codes, we create a pipe class which implements interface: PipeTransform
.
Also we use DatePipe
to format the date object.
//Skip...
import {WrapEventPipe} from './customer.pipe';
@NgModule({
imports: [
BrowserModule,
HttpModule,
CustomerRoutes
],
declarations: [CustomerAppComponent, CustomerIndexComponent, CustomerCreateComponent, CustomerEditComponent, CustomerDetailComponent, WrapEventPipe],
bootstrap: [CustomerAppComponent]
})
export class CustomerAppModule { }
<div *ngIf="events">
<div class="list-group" *ngFor="let evn of events; let sn=index">
<a href="#" class="disabled list-group-item">{{evn | wrapEvent}}</a>
</div>
</div>
We assigned the localization in the constructor of DatePipe
.
var datePipe = new DatePipe("en-US");
We will refactor this and inject the localization setting from customer.app.module
.
//Skip...
import { NgModule, LOCALE_ID } from '@angular/core';
import { WrapEventPipe } from './customer.pipe';
@NgModule({
imports: [
BrowserModule,
HttpModule,
CustomerRoutes
],
declarations: [CustomerAppComponent, CustomerIndexComponent, CustomerCreateComponent, CustomerEditComponent, CustomerDetailComponent, WrapEventPipe],
providers: [
//replace "en-US" with your locale
{ provide: LOCALE_ID, useValue: "en-US" },
],
bootstrap: [CustomerAppComponent]
})
export class CustomerAppModule { }
Update our WrapEventPipe
in order to inject LOCAL_ID
.
import { Pipe, PipeTransform, Inject, LOCALE_ID } from '@angular/core';
//Skip ...
@Pipe({
name: 'wrapEvent'
})
export class WrapEventPipe implements PipeTransform {
constructor( @Inject(LOCALE_ID) private _locale: string) {
}
transform(content: SysEvent) {
//...
//Convert date to string
//var datePipe = new DatePipe("en-US");
var datePipe = new DatePipe(this._locale); //Use the injected LOCAL_ID
//...
}
}
The result is the same as the previous result. (Of course!)
Before we start the next angular 2 tutorial, a good logging framework is a must!
We will create the logger for both frontend and backend.