Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS digits in $index

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

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

The result would be

1 2 3 4 5 6 ...

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

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

like image 320
dk123 Avatar asked Sep 24 '26 00:09

dk123


1 Answers

You could do this pretty easily with a filter.

First, define it like so:

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

Then you can pipe $index into it:

<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/

like image 55
Michelle Tilley Avatar answered Sep 26 '26 13:09

Michelle Tilley