如何在 Angularjs 中将计数器值显示为 2 位数字?

How to display counter value as 2 digits in Angularjs?

我正在使用 angularjs 计时器来构建倒计时显示。

定时器指令returns 小时、分钟和秒的整数值。 意思是 1,2,3,.... 但我想将这些值显示为 2 位数字。 比如,01,02,03,04,......

是否有任何过滤器或指令可以完成此操作?

我想创建一个简单的自定义 AngularJS filter

Filters are used for formatting data displayed to the user.

用法

{{value | counterValue}}

过滤器

app.filter('counterValue', function(){
   return function(value){
     value = parseInt(value);
      if(!isNaN(value) && value >= 0 && value < 10)
         return "0"+ valueInt;
      return "";
   }
})

过滤器的替代方法是自定义指令:

用法

<li ng-repeat="item in array">
    <digit content="item"></digit>
</li>

指令

.directive('digit', function () {
    return {
        restrict: 'E',
        scope: {
            content: '='
        },
        link: function (scope) {
            if (scope.content < 10) {
                scope.zero = "0";
            } else {
                scope.zero = "";
            }
        },
        transclude: true,
        template: '{{zero}}{{content}}'
    };
});