ng2 内置pipe 一览

ng2 内置pipe 一览

pipe说明:

  1. UpperCasePipe --- Transforms text to uppercase.
  2. LowerCasePipe --- Transforms text to lowercase.
  3. JsonPipe --- Converts value into JSON string.
  4. CurrencyPipe --- Formats a number as currency using locale rules.
  5. DatePipe --- Formats a date according to locale rules.
  6. SlicePipe --- Creates a new List or String containing a subset (slice) of the elements.

  1. TitleCasePipe --- Transforms text to uppercase.
  2. PercentPipe --- Formats a number as a percentage according to locale rules.
  3. DecimalPipe --- Formats a number according to locale rules.
  4. AsyncPipe --- Unwraps a value from an asynchronous primitive. observable_or_promise_expression | async

和ng1对比如下:



总结:

  1. ng1中称为filter,ng2中称为pipe 功能类似
  2. ng1中的 limitTo被替换为 slicePipe
  3. ng2 新增了 asyncPipe,decimalPipe等

  • digitInfo is a string which has a following format:

  • {minIntegerDigits}.{minFractionDigits}-{maxFractionDigits}

  • (数值位数限制,整数最小位数.小数最小位数-小数最大位数)

  • minIntegerDigits is the minimum number of integer digits to use. Defaults to 1. 整型数最小的位数

  • minFractionDigits is the minimum number of digits after fraction. Defaults to 0. 小数点后最小的位数

  • maxFractionDigits is the maximum number of digits after fraction. Defaults to 3. 小数点后最大的位数

      <p>{{'abcd' | uppercase}}</p>
      <p>{{'ABcd' | lowercase}}</p>
      <p>{{'{a:1}' | json}}</p>
      <p>{{'1000' | currency}}</p>
      <p>{{'1000' | currency:CNY:true}}</p>
      <p>{{date | date:'y-MM-d'}}</p>
      <p>{{'ABcd' | slice:0:2}}</p>
      <hr>
      <p>{{'abDfEf' | titlecase}}</p>
      <p>{{0.2611123 | percent}}</p>
      <p>{{0.26 | percent:'.2'}}</p>
      <p>{{0.262323 | percent:'.2-4'}}</p> 
    

结果如下:

![](http://upload-images.jianshu.io/upload_images/3915498-364df115665cba9b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

AsyncPipe例子如下:

    import { Component, OnInit } from '@angular/core';

    @Component({
      selector: 'app-pipes-app-component',
      templateUrl: './pipes-app-component.component.html',
      styleUrls: ['./pipes-app-component.component.css']
    })
    export class PipesAppComponentComponent implements OnInit {
      promise:Promise<any>;
      constructor() {
        this.promise = new Promise(function(resolve, reject) {
          setTimeout(function() {
            resolve("Hey, I'm from a promise.");
          }, 2000)
        });
      }
    
      ngOnInit() {
      }
    
    }


    <p>
      pipes-app-component works!
    </p>
    <p>{{ promise | async}}</p>

两秒后显示:Hey, I'm from a promise.

参考:

https://auth0.com/blog/angular2-series-working-with-pipes/

https://angular.io/api/common/AsyncPipe

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,959评论 0 23
  • 1.微阅读:本周打卡7次,坚持每天读3页书的微习惯持续124天。本周读《明心禅》4次,读《与成功有约》2次,读《沟...
    了凡工作室阅读 236评论 0 0
  • 这张图狠狠的说明了一个问题:大部分人能坚持挤公交地铁打卡上班30年,却无法坚持创业3年去获得27年的自由。万一失败...
    黄祥阅读 134评论 0 0
  • 为什么想要开始写作? 每个人都有表达的欲望,我也不例外。 而写作就是表达的一种。从小我就特别羡慕贴吧上那些文笔很好...
    Zoe_ZY阅读 492评论 0 3