2

I am using angularjs timer to build a countdown display.

The timer directive returns value of hours, minutes and seconds as integer. Means, 1,2,3,.... But I want to display these values as 2 digits. like, 01,02,03,04,......

Is there any filter or directive to accomplish this?

2 Answers 2

7

I would like to create a simple custom AngularJS filter.

Filters are used for formatting data displayed to the user.

Usage

{{value | counterValue}}

Filter

app.filter('counterValue', function(){
   return function(value){
     value = parseInt(value);
      if(!isNaN(value) && value >= 0 && value < 10)
         return "0"+ valueInt;
      return "";
   }
})
Sign up to request clarification or add additional context in comments.

Comments

1

The alternative to the filter would be the custom directive:

Usage

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

Directive

.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}}'
    };
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.