本文介绍了如何在angular2 / ionic 2打字本应用程序中使用Numeral.js库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在angularJs(1.x)应用程序中使用Numeral.js库成功地以不同格式格式化我的数字。这是我使用angular1过滤器的方式:

I have been successful using Numeral.js library in my angularJs(1.x) applications to format my numbers in different formats. This is how I use angular1 filters:

filter.js

filter.js

.filter('numeral', function () {
  return function (count) {
    var numericalFormat = numeral(count).format('0.0a');
    return numericalFormat;
  };
})

为此,我跟着官方
并使用npm安装。我还在index.html中引用了nodemodules。

For that I followed the official docs of Numeral.jsand installed using npm. I also give reference to nodemodules in index.html.

在浏览器中

<script src="node_modules/numeral/numeral.min.js"></script>

但它显示

最初我尝试使用CDN参考,它按照我在浏览器中的预期工作。但是当我在真实设备上安装应用程序时,我收到错误数字未定义。

Initially I tried with using CDN reference, it works as I expected in the browser. But when I installed the app on real device I am getting an error "numeral is not defined".

pipe

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中做到这一点的方法?

Is there any type script library for formatting and manipulating numbers, or is there any way to do this in angular2?

推荐答案

添加

声明var数字:任何

import {Pipe,PipeTransform,Injectable} from "@angular/core".

这篇关于如何在angular2 / ionic 2打字本应用程序中使用Numeral.js库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 16:52