First, how to use a build in pipe:

            <div class="pipe-example">
<label>Uppercase Pipe: {{ message | uppercase }}</label>
</div> <div class="pipe-example">
<label>Lowercase Pipe: {{ message | lowercase }}</label>
</div> <div class="pipe-example">
<label>Slice Pipe: {{ message | slice:0:5 }}</label>
</div> <div class="pipe-example">
<label>Date Pipe: {{ date | date:'yyyy-MMM-dd' }}</label>
</div> <div class="pipe-example">
<label>Number Formatting: {{ pi | number:'5.1-2' }}</label>
</div> <div class="pipe-example">
<label>Percent Pipe: {{ percentage | percent:'2.1-2' }}</label>
</div> <div class="pipe-example">
<label>Currency Pipe: {{ amount | currency:'USD':true:'2.1-2' }}</label>
</div>

Then how to create a custom pipe:

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

@Pipe({
name: 'sort'
})
export class SortPipe implements PipeTransform {
transform(value: any[], args): any[] { let sorted = [
...value
].sort(); if(args.length > 0 && args === "DESC"){
sorted = sorted.reverse();
} return sorted;
}
}

Notice here, we use:

let sorted = [
...value
].sort();

The reason doing this is because sort() is mutate opreation, once you apply sort(), it will mutate the original data, which is not good. So here we create a copy of data and apply sort() fucntion to enhance immutatbility.

Pure and Impure:

See example at: https://angular.io/resources/live-examples/pipes/ts/plnkr.html

Pure pipe it means:

It won't go thought Angular Change Detection if the primitive type reference doesn't change.

    if (this.mutate) {
// Pure pipe won't update display because heroes array reference is unchanged
// Impure pipe will display
this.heroes.push(hero);
} else {
// Pipe updates display because heroes array is a new object
this.heroes = this.heroes.concat(hero);
}

So if 'this.mutate' is ture, then we mutate this.heros array, so primitive type (array) reference is changed. So this will trigger change detection. So that, everytime you add a hero into the this.hero, piped result will update.

If this.mutate is false, then reference won't change, therefore,  if you add new hero, even set 'can fly' tag to ture, it won't update the piped result.

Impure pipe:

Angular executes an impure pipe during every component change detection cycle. An impure pipe will be called a lot, as often as every keystroke or mouse-move.

To set pure or impure you just need to add a pure flag:

@Pipe({
name: 'sort',
pure: true //false
})

Default is ture.

So when build a custom pipe, you need to decide whether it should be pure or impure.

[Angular 2] Understanding Pure & Impure pipe的更多相关文章

  1. [Angular] Increasing Performance by using Pipe

    For example you make a function to get rating; getRating(score: number): string { let rating: string ...

  2. [Angular] Bind async requests in your Angular template with the async pipe and the "as" keyword

    Angular allows us to conveniently use the async pipe to automatically register to RxJS observables a ...

  3. angular和ionic4对过滤器pipe的使用

    以下为自定义过滤器 import { Pipe, PipeTransform, Injectable } from '@angular/core'; import { DomSanitizer} fr ...

  4. [Angular 2] Understanding OpaqueToken

    When using provider string tokens, there’s a chance they collide with other third-party tokens. Angu ...

  5. [Angular 2] Understanding @Injectable

    In order to resolve a dependency, Angular’s DI uses type annotations. To make sure these types are p ...

  6. Angular 的性能优化

    目录 序言 变更检查机制 性能优化原理 性能优化方案 小结 参考 序言 本文将谈一谈 Angular 的性能优化,并且主要介绍与运行时相关的优化.在谈如何优化之前,首先我们需要明确什么样的页面是存在性 ...

  7. [转]Angular2 使用管道Pipe以及自定义管道格式数据

    本文转自:https://www.pocketdigi.com/20170209/1563.html 管道(Pipe)可以根据开发者的意愿将数据格式化,还可以多个管道串联. 纯管道(Pure Pipe ...

  8. Angular2 Pipe

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

  9. angular管道相关知识

    原文地址 https://www.jianshu.com/p/22e0f95bcf24 什么是管道 每个应用开始的时候差不多都是一些简单任务:获取数据.转换它们,然后把它们显示给用户. 获取数据可能简 ...

随机推荐

  1. 开源GIS简介

    原文 开源GIS C++开源GIS中间件类库: GDAL(栅格)/OGR(矢量)提供了类型丰富的读写支持 GEOS(Geometry Engine Open Source)是基于C++的空间拓扑分析实 ...

  2. [Papers]NSE, $\p_3u$, Lebesgue space [Penel-Pokorny, AM, 2004]

    $$\bex \p_3\bbu\in L^p(0,T;L^q(\bbR^3)),\quad \frac{2}{p}+\frac{3}{q}=\frac{3}{2},\quad 2\leq q\leq ...

  3. UITableView 点击展开的实现

    推介看下这里的 内容  http://www.cnblogs.com/kenshincui/p/3931948.html IOS8 above UITabliViewCell 利用 autolayou ...

  4. duilib中控件拖拽功能的实现方法(附源码)

    转载请说明原出处,谢谢~~:http://blog.csdn.net/zhuhongshu/article/details/41144283 duilib库中原本没有显示的对控件增加拖拽的功能,而实际 ...

  5. 仿酷狗音乐播放器开发日志十九——CTreeNodeUI的bug修复二(附源码)

    转载请说明原出处,谢谢 今天本来打算把仿酷狗播放列表的子控件拖动插入功能做一下,但是仔细使用播放列表控件时发现了几个逻辑错误,由于我的播放 列表控件是基于CTreeViewUI和CTreeNodeUI ...

  6. uC/OS - III 移植 IAR平台

    关于移植uC/OS-III 网上已经有很多教程了此处只是做个记录 首先下载源码然后解压得到下面的文件: 然后在模版工程里新建各种文件夹: 最后全部都添加进工程: OK了,编译一下,惊呆了,竟然 0错误 ...

  7. 我喜欢的乐队-Euphoria

    来自日本的后摇乐团,001年冬天由森川裕之.佐藤昭太.木下阳辅三人于东京组建,2003年签约日本独立厂牌123Record,并发行首张EP细碟<Floral Dew>.包括EP.Singl ...

  8. 【九度OJ】题目1078-二叉树遍历

    题目 这道题和后面的两道题,题目1201和题目1009,主要内容是对递归的实现,在逻辑上,递归是容易理解的,这种实现方式和我们思考的方式是相吻合的.但是在转换为计算机语言时,要明确告知计算机应该从哪里 ...

  9. fork之后发生了什么(基于3.16-rc4)

    在linux c编程中,我们可以使用fork,vfork,clone三个系统调用来创建子进程.下面我们先分析下fork系统调用的实现原理.代码如下(kernel/fork.c): #ifdef __A ...

  10. Entity Framework Power Tools

    http://visualstudiogallery.msdn.microsoft.com/72a60b14-1581-4b9b-89f2-846072eff19d