我已经成功在我的angularJs(1.x)应用程序中使用Numeral.js库将我的数字设置为不同格式。这就是我使用angular1过滤器的方式:
filter.js
.filter('numeral', function () {
return function (count) {
var numericalFormat = numeral(count).format('0.0a');
return numericalFormat;
};
})
为此,我遵循了Numeral.js的官方文档,并使用npm进行了安装。我也引用index.html中的nodemodules。
在浏览器中
<script src="node_modules/numeral/numeral.min.js"></script>
但它显示
错误ReferenceError:在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中有任何方法可以做到这一点?
加
declare var numeral:any
后
import {Pipe,PipeTransform,Injectable} from "@angular/core".
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句