Angular内置的pipe通常用在template中,好比下面的CurrencyPipe用来格式化货币 <p>A: {{a | currency:'USD':true:'1.0-0'}}</p>
若是变量a的值是2345。格式化后会显示成$2,345
。很是方便。typescript
若是须要在component内使用原生pipe,能够用下面的方法:bash
import {CurrencyPipe} from '@angular/common'
.....
providers: [CurrencyPipe]
复制代码
import {CurrencyPipe} from '@angular/common'
....
constructor(private currencyPipe: CurrencyPipe) { ... }
复制代码
this.value = this.cp.transform(this.value, 'USD': true: '1.0-0'); // $12,345
ngninja.com/posts/angul…angular2