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