zoukankan      html  css  js  c++  java
  • [Angular 2] Understanding Pure & Impure pipe

    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.

  • 相关阅读:
    【LeetCode-字符串】重构字符串
    【LeetCode-二叉树】填充每个节点的下一个右侧节点指针
    【LeetCode-回溯】分割回文串
    【LeetCode-字符串】最后一个单词的长度
    【LeetCode-数组】生命游戏
    【LeetCode-链表】奇偶链表
    【LeetCode-字符串】反转字符串
    【LeetCode-数学】打乱数组
    Java中实现多线程的3种方法介绍和比较
    oracle 临时表空间、数据表空间、创建用户名与密码、赋予用户权限
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5883734.html
Copyright © 2011-2022 走看看