本文介绍了AngularJS在$指数位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说我有以下的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>

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

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/

这篇关于AngularJS在$指数位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 09:34