我正在学习Angular2,我想格式化一个加上一千个逗号分隔符的数字.据我所知,这可以使用Pipes完成,问题是我想在js文件中以编程方式格式化数字,而不是在html中(像var | number一样).
首先,我意识到没有可以使用的NumberPipe独立管道(如果我错了,请纠正我)最相似的是@ angular2/common中的CurrencyPipe.所以我有这样的事情:
import { Component } from '@angular/core';
import { CurrencyPipe } from '@angular/common';
@Component({
templateUrl: 'test.component.html',
styleUrls: ['./test.component.scss']
})
export class TestComponent {
public myNumber = 1000;
constructor(private currencyPipe: CurrencyPipe) {
var formatted = this.currencyPipe().transform(this.myNumber, 'MXN', true); // Is this correct?
}
}
Run Code Online (Sandbox Code Playgroud)
但它会引发以下错误: 未处理的Promise拒绝:没有提供CurrencyPipe!; 区域:棱角分明; ......
我究竟做错了什么?
提前致谢.
问候