在Angular2 模板中,我们在显示数据时,可以使用通道将数据转换成相应的格式的值的形式来显示,而且这不改变源数据。比如,我们可以使用date通道来转换时间显示的格式: {{date | date:'yyyy-MM-dd'}} ,(1) 以下是Angular提供的基本的通道.

Basic Pipes
Pipe Name Usage Parameters or Effection
currency {{test.currency | currency }}

1234569678 > USD1,234,569,678.00

date {{test.date | date:'yyyy-MM-dd'}}

date:'mediumDate',//Jan,9,2017

date:'shortTime',//9:36 pm

json {{test.json | json }}
[object Object] > {
"name": "test",
"title": "Test Pipes"
} //with json pipe
uppercase {{test.str | uppercase }}  hello pipes > HELLO PIPES
lowercase {{test.str1 | lowercase }}  Hello > hello
percent {{test.percent | percent:'.3' }} percent:'.3'//0.25 > 25.000% 
number {{test.decimal | number:'5.1-2' }} number:'5.1-3'//23440.0987 > 23,440.099

(2) 以下自定义了一转换电话号码指定数位尾号之外的数字的Pipe, +008615527370515 > ***********70515

//tail-number-length.pipe.ts
import { Pipe,PipeTransform } from '@angular/core'; @Pipe({
//通道名称
name:'tailNumberLength',
pure:false
})
export class TailNumberLengthPipe implements PipeTransform{
transform(value: string, length:number): any {
//对源数据的转换方法
let tailNumberLength = (length === undefined ? 4 : length);
let prefixLength = value.length - tailNumberLength;
let target = '';
for(let index = 0;index < prefixLength; ++index){
target += '*';
}
target += value.substr(prefixLength);
return target;
}
}

  

//tail-number-length.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { TailNumberLengthPipe } from './tail-number-length.pipe'; 构建一个NgModule装饰的TailNumberPipeModule
@NgModule({
declarations:[
//引入TailNumberLengthPipe
TailNumberLengthPipe
],
imports:[
CommonModule
],
exports:[
TailNumberLengthPipe
]
}) export class TailNumberPipeModule{};
appComponent.module.ts
@NgModule({
bootstrap: [ AppComponent ],
imports: [
// import custom modules
TailNumberPipeModule
]
})
{{person.phone | tailNumberLength:5}}//+008615527370515 > ***********70515

  

Angular2 - Starter - Pipes, Custom Pipes的更多相关文章

  1. [Angular2 Form] Create custom form component using Control Value Accessor

    //switch-control component import { Component } from '@angular/core'; import { ControlValueAccessor, ...

  2. Angular2 - Starter - Component and Component Lifecircle Hooks

    我们通过一个NgModule来启动一个ng app,NgModule通过bootstrap配置来指定应用的入口组件. @NgModule({ bootstrap: [ AppComponent ], ...

  3. Angular2 - Starter - Routes, Route Resolver

    在基于Angualr的SPA中,路由是一个很重要的部分,它帮助我们更方便的实现页面上区域的内容的动态加载,不同tab的切换,同时及时更新浏览器地址栏的URN,使浏览器回退和前进能导航到历史访问的页面. ...

  4. Haskell语言学习笔记(83)Pipes

    安装 pipes $ cabal install pipes Installed pipes-4.3.9 Prelude> import Pipes Prelude Pipes> impo ...

  5. python标准库介绍——35 pipes 模块详解

    ==pipes 模块== (只用于 Unix) ``pipes`` 模块提供了 "转换管道 (conversion pipelines)" 的支持. 你可以创建包含许多外部工具调用 ...

  6. 【计算几何】【二分图判定】Gym - 101485C - Cleaning Pipes

    题意:有n个水井,每个水井发出一些管线(都是线段),然后每条管线上最多只有一个水井.所有从不同的水井发出的管线的相交点都是清洁点(不存在清洁点是大于两条管线点的交点).你需要在某些管线上放出一些机器人 ...

  7. ASP.NET MVC和Web API中的Angular2 - 第2部分

    下载源码 内容 第1部分:Visual Studio 2017中的Angular2设置,基本CRUD应用程序,第三方模态弹出控件 第2部分:使用Angular2管道进行过滤/搜索,全局错误处理,调试客 ...

  8. Angular2 Pipe

    AngularJs 1.x 中使用filters来帮助我们转换templates中的输出,但在Angular2中使用的是pipes,以下展示Angular 1.x and Angular 2中filt ...

  9. 网络-05-端口号-F5-负载均衡设-linux端口详解大全--TCP注册端口号大全备

    [root@test1:Standby] config # [root@test1:Standby] config # [root@test1:Standby] config # [root@test ...

随机推荐

  1. div 居中进行总结

    1.margin:auto ;让元素居中,需要确定元素的宽度,并且需要是块元素 eg: div { width:200px; height:200px; background:#222; margin ...

  2. activiti 5.17 流程图中文乱码问题

    1. 流程图中任务中的中文乱码显示问题.   解决方法:设置processEngineConfiguration中的两个字体属性,例如: <bean id="processEngine ...

  3. Ubuntu12.04 下svn服务搭建及Windows客户端tortoisesvn的使用

    在Ubuntu服务端搭建apache+svn 在客户端使用Tortoisesvn工具. 第一步 安装SVN $sudo apt-get install subversion 安装成功后系统会自动建立一 ...

  4. iotop使用

    介绍 Linux下的IO统计工具如iostat, nmon等大多数是只能统计到per设备的读写情况, 如果你想知道每个进程是如何使用IO的就比较麻烦. iotop 是一个用来监视磁盘 I/O 使用状况 ...

  5. 【转】将Vim改造为强大的IDE—Vim集成Ctags/Taglist/Cscope/Winmanager/NERDTree/OmniCppComplete(有图有真相)

    原文网址:http://blog.csdn.net/bokee/article/details/6633193 工欲善其事,必先利其器.一个强大的开发环境可以大大提高工作效率.好吧,我知道这是废话.. ...

  6. STL中,back_insert_iterator与back_inserter的区别。

    1.参考http://www.cplusplus.com网站关于back_insert_iterator与back_inserter的介绍之后,我总算明白了:back_insert_iterator, ...

  7. java反编译工具(XJad)

    java反编译工具(XJad) 2.2 绿色版 http://www.cr173.com/soft/35032.html Demo.class     --->    Demo.java

  8. poj 2456 Aggressive cows(二分搜索之最大化最小值)

    Description Farmer John has built a <= N <= ,) stalls. The stalls are located along a straight ...

  9. Vagrant入门[转]

    Vagrant是一个简单易用的部署工具,用英文说应该是orchestration tool.它能帮助开发人员迅速的构建一个开发环境,帮助测试人员构建测试环境. Vagrant的基本工作原理大致如下: ...

  10. HDU 3265 Posters(线段树)

    HDU 3265 Posters pid=3265" target="_blank" style="">题目链接 题意:给定一些矩形海报.中间有 ...