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. Selenium2Library系列 keywords 之 _SelectElementKeywords 之 get_selected_list_values(self, locator)

    def get_selected_list_values(self, locator): """Returns the values of selected elemen ...

  2. 初识MFC,WinForm,WPF,Q&#39;t

    MFC和QT是C++中常见的GUI框架,而WinForm和WPF是C#中常用的框架,不过我们一般很少叫WinForm框架,可能直接叫图形控件类库更多点.反正只是个称呼罢了,爱咋叫就咋叫.另外WinFo ...

  3. Java-note-字符串连接

    String a="100"; int b=2; String c=a+b; then the answer is c=1002; + make the two sides bec ...

  4. 只有IE能上网,其他浏览器均不可以!

    今天起来,高高兴兴的来到实验室,发现电脑打开上不了网.本人平时喜欢用Chrome,发现上不了网,就开始ping.发现可以ping通,但是网页打不开! 第一反应是DNS的问题,可以发现DNS没问题,能正 ...

  5. php环境配置中各个模块在网站建设中的功能

    上一篇配置环境的时候,我们注意到,有四个模块需要配置,那么,这四个模块分别有哪些功能呢?   一.php php是我们的用来创建动态网页的强有力的脚本语言,安装过程中我们直接解压到某一个路径就好了,比 ...

  6. GridView控件 Reapter控件 DataList控件 的区别和用法

    ASP.NET三大控件: 1.GridView控件:表格视图控件,可以用来绑定结果集或者视图,用起来比较方便和灵活,三个控件中使用最多的控件 用法--- this.gridview1.DataSour ...

  7. Can’t find file mysql/host.frm

    安装Mysql后报错: InnoDB: Apply batch completed 141115 15:04:36 InnoDB: Started; log sequence number 0 442 ...

  8. openstack domain serverID connect uri

  9. Spark生态之Spark MLbase/MLlib

  10. HDU 1856 More is better(并查集)

    http://acm.hdu.edu.cn/showproblem.php?pid=1856 More is better Time Limit: 5000/1000 MS (Java/Others) ...