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)
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With