【发布时间】:2017-06-12 07:12:11
【问题描述】:
我已经成功地在我的 angularJs(1.x) 应用程序中使用 Numeral.js 库将我的数字格式化为不同的格式。这就是我使用 angular1 过滤器的方式:
filter.js
.filter('numeral', function () {
return function (count) {
var numericalFormat = numeral(count).format('0.0a');
return numericalFormat;
};
})
为此我关注了官方docs of Numeral.js 并使用 npm 安装。我还在 index.html 中引用了 nodemodules。
在浏览器中
<script src="node_modules/numeral/numeral.min.js"></script>
但它显示
ERROR ReferenceError: number is not defined 在 NumeralFilter.transform
最初我尝试使用 CDN 引用,它在浏览器中按预期工作。但是当我在真实设备上安装应用程序时,我收到一个错误“未定义数字”。
管道
import {Pipe,PipeTransform,Injectable} from "@angular/core"
@Pipe({
name:'numeralPipe'
})
@Injectable()
export class NumeralPipe implements PipeTransform{
transform(count:any):any{ //eg: in count has value something like 52456.0
var numericalFormat = numeral(count).format('0.0a');//error here
return numericalFormat; // i have to format it like 52,5k
};
}
是否有任何类型脚本库用于格式化和操作数字,或者在 angular2 中有什么方法可以做到这一点?
【问题讨论】:
-
尝试在
import {Pipe,PipeTransform,Injectable} from "@angular/core"之后添加declare var numeral:any -
添加为答案。你可以接受和支持
标签: angular ionic2 angular-pipe numeral.js