0

I am trying to print numbers from 0 to 24 mod 12 using ng-repeat. But I want the numbers to be displayed as

00 01 02 03 04 .... 11 00 01 02 ... 11

and not

0 1 2 3... 11 0 1 2 .... 11

Can this be done in angular ?

Kyuubi
  • 1,228
  • 3
  • 18
  • 32
  • Take a look at this question: http://stackoverflow.com/questions/10073699/pad-a-number-with-leading-zeros-in-javascript – knutesten Sep 05 '14 at 10:35

3 Answers3

1

You could define a 'zeroPad' filter, like this:

angular.module('myModule', [])
    .filter('zeroPad', function () {
        return function (n, length) {
            var num = parseInt(n, 10);
            length = parseInt(length, 10);
            if (isNaN(num) || isNaN(length)) {
                return n;
            }
            num = '' + num;
            while (num.length < length) {
                num = '0' + num;
            }
            return num;
        };
    });

And then use it in your view this way:

{{ myNumber | zeroPad: 2 }}
MarcoS
  • 17,323
  • 24
  • 96
  • 174
0

Here what I have done for you: http://plnkr.co/edit/BlodIO1TvG3uGzncjM75?p=preview

Make a custom filter option like this:

    var app = angular.module('plunker', []);

    app.controller('MainCtrl', function($scope) {
      $scope.name = 'World';

      $scope.numbers = [];

      for (var i = 1; i < 24; i++)
        $scope.numbers.push(i);
    });


    app.filter('padding', function() {
      return function(input) {
        var n = input;
        return (n < 10) ? '0' + n : n;
      }
    });

And in HTML page:

<ul>
  <li ng-repeat="number in numbers">
    {{number % 12 | padding}}
  </li>
</ul>

Please mark as answer if it answers your question

masum7
  • 822
  • 6
  • 17
0

Minimum code with _.str angular padding filter:

jsFiddle

<div ng-app="app" ng-controller="PadController">
   <div ng-repeat="num in nums">{{ num | _.str: 'pad':[2, '0'] }}</div>
</div>
angular.module('app', ['underscore.string']).controller('PadController', function ($scope) {
    $scope.nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
});
Benny Bottema
  • 11,111
  • 10
  • 71
  • 96