AngularJS在$指数位数位数、指数、AngularJS

由网友(深情.)分享简介:比方说我有以下的code(伪):Say for example I have the following code (pseudo):{{ $index }}其结果将是1 2 3 4 5 6 ... 我怎么能去改变以上,这样的指标印在code How co...

比方说我有以下的code(伪):

Say for example I have the following code (pseudo):

<div ng-repeat=range>{{ $index }}</div>

其结果将是

1 2 3 4 5 6 ...

我怎么能去改变以上,这样的指标印在code

How could I go about changing the code above so that the indexes are printed

001 002 003 004 005 006 ? (这样的索引打印为3位)

001 002 003 004 005 006? (so that the index is printed as 3 digits)

推荐答案

您可以很容易地过滤器做pretty。

You could do this pretty easily with a filter.

首先,定义它像这样:

myModule.filter("tripleDigit", function() {
  return function(number) {
    if (number !== null && number !== undefined) {
      var str = "" + number;
      while (str.length < 3) str = "0" + str;
      return str;
    }
  };
});

然后你可以管 $指数到它:

<div ng-repeat=range>{{ $index | tripleDigit }}</div>

下面是它定义了一个名为过滤器,可以让你在多少位数字垫作为参数传递一个例子:的 http://jsfiddle.net/BinaryMuse/64ycY/

Here's an example that defines a filter called pad that allows you to pass in how many digits to pad as an argument: http://jsfiddle.net/BinaryMuse/64ycY/

阅读全文

相关推荐

最新文章