This article mainly introduces the usage of Angular2 pipeline Pipe and custom pipeline format data. It analyzes the related concepts, syntax and usage skills of Angular2 pipeline and pure pipeline in detail in the form of examples. Friends in need can refer to it
The example in this article describes the usage of Angular2 pipeline Pipe and custom pipe format data. Share it with everyone for your reference, the details are as follows:
Pipe can format data according to the developer's wishes, and can also connect multiple pipes in series.
Pure Pipe and Impure Pipe
Pipeline is divided into Pure Pipe and Impure Pipe (Impure Pipe). By default, pipes are pure. If you set the pure flag to false when declaring a custom pipe, it means that the pipe is not pure. For example:
@Pipe({
name: 'sexReform',
pure:false
})
The difference between pure pipes and non-pure pipes:
①Pure pipe:
Angular only detects pure changes in the input value , pure pipeline will be executed. Pure changes refer to changes in original type values (String,Number,Boolean,Symbol) or changes in object references. (Changes in object values are not pure changes and will not be executed).
② Impure pipeline
Angular will execute impure pipelines in the change detection cycle of each component. Therefore, if we use impure pipes, we have to pay attention to performance issues.
Pipe usage syntax
{{expression | pipe : arg}}
If it is chain concatenation:
{{expression | pipe1 : arg | pipe2 | pipe3 }}
Commonly used built-in pipes
Pipe |
Type |
Function |
##DatePipe | Pure Pipe | Date formatting |
JsonPipe | Non-pure pipe | Use JSON.stringify() to convert the object into json characters String |
UpperCasePipe | Pure Pipe | Convert all letters in the text to uppercase |
LowerCasePipe | Pure Pipe | Convert all letters in the text to lowercase |
DecimalPipe | Pure Pipe | Numeric formatting |
CurrencyPipe | Pure pipe | Currency formatting |
PercentPipe | Pure pipe | Percent formatting |
SlicePipe | Non-pure pipe | Array or string cutting |
DatePipe
语法:{{expression | date:format}}
expression支持日期对象、日期字符串、毫秒级时间戳。format是指定的格式,常用标志符:
y 年 y使用4位数字表示年份(2017),yy使用两位数字表示(17)
M 月 M 1位或两位数字(2或10、11、12),MM 两位数字表示,前面补0(02)
d 日 d 一位或两位数字(9) dd两位数字,前面补0(09)
E 星期 EEE 三位字母缩写的星期 EEEE 星期全称
j 12小时制时间 j (9 AM) jj (09 AM)
h 12小时制小时 h(9) hh (09)
H 24小时制小时 H(9) HH (09)
m 分 m (5) mm (05)
s 秒 s (1) ss (01)
z 时区 z China Standard Time
DecimalPipe
语法:{{expression | number[: digiInfo] }}
digiInfo格式:
{minIntegerDigits}.{minFractionDigits}-{maxFractionDigits}
即:整数位保留最小位数.小数位保留最小位数-小数位最大保留位置
默认值: 1.0-3
CurrencyPipe
语法:{{expression | currency[: currencyCode[: symbolDisplay[: digiInfo]]] }}
digiInfo格式与DecimalPipe相同,不再解释。
currencyCod是指货币代码,其值为ISO 4217标准,人民币CNY,美元USD,欧元 EUR.
symbolDisplay 是一个布尔值,true时显示货币符号($¥) false显示货币码
PercentPipe
语法:{{expression | percent[: digiInfo] }}
digiInfo格式与DecimalPipe相同,不再解释。
SlicePipe
语法:{{expression | slice: start [: end] }}
expression 可以是一个字符串或数组。字符串时,该管道调用String.prototype.slice()
方法截取子串。如果是数组,调用Array.prototype.slice()
方法取数组子元素。
自定义管道
除了使用内置的管道,还可以通过自定义管道实现更复杂的功能。
创建管道:
ng g pipe sexReform
angular-cli会帮我们创建SexReformPipe管道,这个管道的功能是根据male、female返回中文的男、女。
代码:
import {Pipe, PipeTransform} from '@angular/core';
@Pipe({
name: 'sexReform',
//非纯管道
pure:false
})
export class SexReformPipe implements PipeTransform {
transform(value: any, args?: any): any {
let chineseSex;
switch (value) {
case 'male':
chineseSex = '男';
break;
case 'female':
chineseSex = '女';
break;
default:
chineseSex = '未知性别';
break;
}
return chineseSex;
}
}
重点在于实现PipeTransform接口的transform方法,定义为非纯管道仅用于演示,非纯管道对性能影响较大,尽量避免。
演示代码
组件:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-pipe',
templateUrl: './pipe.component.html',
styleUrls: ['./pipe.component.css']
})
export class PipeComponent implements OnInit {
date=new Date();
money=5.9372;
object={title:'ffff',subTitle:'subtitlefff'};
str='abcdABCD';
percent=0.97989;
constructor() { }
ngOnInit() {
}
}
模板:
<p>
{{date| date:'y-MM-dd HH:mm:ss'}} <br />
{{object| json }} <br />
{{str| uppercase }} <br />
{{str| lowercase }} <br />
{{money| number:'2.4-10' }} <br />
{{money| number:'5.1-2' }} <br />
{{money| currency:'CNY':false:'1.1-2' }} <br />
{{percent| percent:'1.1-2' }} <br />
{{str| slice:1:3 }} <br />
{{'female'| sexReform }} <br />
</p>
上面是我整理给大家的,希望今后会对大家有帮助。
相关文章:
在微信小程序中如何实现点赞功能
有关JavaScript数组操作难点(详细教程)
使用vue如何实现无缝滚动组件
The above is the detailed content of About custom pipe format data usage in Angular2. For more information, please follow other related articles on the PHP Chinese website!