美文网首页
Angular—pipe

Angular—pipe

作者: 芝麻香油 | 来源:发表于2019-03-25 15:14 被阅读0次

什么是pipe

中文释义“管道”。

把数据作为输入,然后转换它,给出期望的输出。例如,将UTC时间转为指定格式;中英文的转化;大小写的转化;等等。

pipe 的使用

<p>
  {{'HELLO-WORLD WORKS!' | lowCase}}
</p>

链式pipe

<p>
  {{'HELLO-WORLD WORKS!' | lowCase | upperCase}}
</p>

自定义 pipe

  • 创建pipe命令:
    ng generate pipe pipe-name

  • 写实现

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

@Pipe({
  name: 'lowCase'
})
export class LowCasePipe implements PipeTransform {

  transform(value: any): any {
    return value.toLowerCase();
  }

}
  • 使用 pipe
<p>
  {{'HELLO-WORLD WORKS!' | lowCase}}
</p>

附录

  1. Angular 内置了一些管道

    • DatePipe
    • UpperCasePipe
    • LowerCasePipe
    • CurrencyPipe
    • PercentPipe
    • ...
  2. @pipe这个注解里有两个参数,即:

@Pipe({
  name: string
  pure?: boolean
})

其中name就是该pipe的名字,而pure表示该pipe是纯pipe还是非纯pipe。

  • 纯pipe:只有在它检测到输入值发生了纯变更时才会执行
  • 非纯pipe:在每个组件的变更检测周期中执行

自定义pipe默认都是非纯pipe。

相关文章

  • Angular Pipe

    Angular Pipe is object, not function, when pipe created, ...

  • Angular—pipe

    什么是pipe 中文释义“管道”。 把数据作为输入,然后转换它,给出期望的输出。例如,将UTC时间转为指定格式;中...

  • Angular 关于pipe

    Angular 中的管道其实就是angularjs中的过滤器,用来转换数据然后显示给用户。要创建一个管道,必须实现...

  • new Date(date)在ios下的兼容性问题。

    附上moment.js及Angular@Pipe相关链接: http://momentjs.cn/https://...

  • angular pipe 自定义管道

    可以理解为angular中的管道(pipe)与angular1.x的过滤器(filter)一样。 那么我们就来自定...

  • angular component direct pipe

    组件 声明一个组件 定义完组件需要在app.module declarations引入 cli创建组件 利用命令创...

  • Angular 单元测试实践 (4)

    本文继续介绍如何对 Angular 的管道(pipe)、指令(directive)和表单(form)进行单元测试。...

  • Angular component 组件内使用原生pipe

    Angular内置的pipe一般用在template中,比如下面的CurrencyPipe用来格式化货币 A: {...

  • Angular自定义pipe实现

    angular尽管内置许多的pipe,比如date、async、currency等。尽管拿来即用,但是远不能满足于...

  • angular2管道pipe

    谁用angular2自定义管道写过过滤器,搜索类似于angular1中firter的功能?急急

网友评论

      本文标题:Angular—pipe

      本文链接:https://www.haomeiwen.com/subject/hldlmqtx.html