我刚接触到angular2 . 尝试使用angular实现一些基本的东西 .
使用内置的角度货币格式化程序时出错 . 我知道有些API在最新版本的angular2中更新,因此格式化程序不适用于某些浏览器 . 在Chrome中,一切正常 .
使用内置格式化程序: -
<span class="badge">{{product.price | currency}}</span>
作为我使用自定义格式化程序实现的解决方法,我也遇到了同样的问题
import { Pipe, PipeTransform } from '@angular/core';
import { CurrencyPipe } from '@angular/common';
@Pipe({
name: 'customCurrencyFormatter'
})
export class CustomCurrencyFormatter implements PipeTransform {
transform(value: number, currencyCode: string = 'BRL', symbolDisplay: boolean = true, digits?: string): string {
if (!value) {
return '';
}
let currencyPipe: CurrencyPipe = new CurrencyPipe('pt-BR');
let newValue: string = currencyPipe.transform(value, currencyCode, symbolDisplay, digits);
return newValue;
}
}
用法:-
<span class="badge">{{product.price | customCurrencyFormatter}}</span>
附上下面的截图 . 请帮帮我 .
提前致谢 .
1 回答
Date和Currency管道需要ECMAScript Internationalization API . Safari和其他旧版浏览器不支持它 . 我们可以添加polyfill支持 .
在您的html页面中包含此脚本 .