AngularJS counter to count up to a target number

Problem

I'm new to Angular, and would like to implement the same easy function extension in JQuery, but use directive (as far as i understand this is how it supposed to be done).

does somone know ready implimentation?

my search ended up only with JQuery solutions and i don't know how to convert it to Angular.

this is what i needed to do:

link to example: http://jsfiddle.net/YWn9t/

can you help?

function declaration:

$.fn.countTo.defaults = {
    from: 0,  // the number the element should start at
    to: 100,  // the number the element should end at
    speed: 1000,  // how long it should take to count between the target numbers
    refreshInterval: 100,  // how often the element should be updated
    decimals: 0,  // the number of decimal places to show
    onUpdate: null,  // callback method for every time the element is updated,
    onComplete: null,  // callback method for when the element finishes updating
};

how to use:

jQuery(function($) {
        $('.timer').countTo({
            from: 50,
            to: 2500,
            speed: 5000,
            refreshInterval: 50,
            onComplete: function(value) {
                console.debug(this);
            }
        });
    });

html:

<span class="timer"></span>

taken from: stackoverflow

Problem courtesy of: Liad Livnat

Solution

Look at this directive http://siddii.github.io/angular-timer/ . I believe this meets all your requirements.

Solution courtesy of: Chandermani

Discussion

Well it didn't worked for me, i didn't find the right implementation but it helps me to implement my own directive.

html:

<count-up count-to="1000" interval="1"></count-up>

directive.js

directive('countUp', ['$compile',function($compile,$timeout) {
    return {
        restrict: 'E',
        replace: false,
        scope: {
            countTo: "=countTo",
            interval: '=interval'
        },
        controller: ['$scope', '$element', '$attrs', '$timeout', function ($scope, $element, $attrs, $timeout) {
            $scope.millis = 0;
            if ($element.html().trim().length === 0) {
                $element.append($compile('<span>{{millis}}</span>')($scope));
            } else {
                $element.append($compile($element.contents())($scope));
            }

            var i=0;
            function timeloop () {
                setTimeout(function () {
                    $scope.millis++;
                    $scope.$digest();
                    i++;
                    if (i<$scope.countTo) {
                        timeloop();
                    }
                }, $scope.interval)
            }
            timeloop();
        }]
    }}])
Discussion courtesy of: Liad Livnat

https://github.com/Kop4lyf/angular-animation-counter

Although not as general as http://siddii.github.io/angular-timer/, it is small and serves the basic purpose.

Hope it helps.

Discussion courtesy of: Kop4lyf

Since looks like nobody was able to provide a simple and easy to use solution without including a huge dependency and providing readable / quality code. Heres a super simple directive for angular 1.6.x that utilizes interpolation.

HTML

<ng-Counter target="mymodel.countvalue" speed="10" start="mymodel.startfromvalue"/>

This one has 3 attributes:

  • target the number to reach
  • speed the speed..
  • start the number to start from

It will handle both count up & down. Also automatically starts counting whenever the target model is updated, if you define the start then it will reset the counter whenever its updated.

ngCounter.js:

app.directive("ngCounter", function()
{
    return {
        restrict: 'E',
        template: "<span>{{value | number:0}}</span>",
        scope: {
            target: "=",
            speed: "=?",
            start: "=?",
        },
        link: function ($scope, $element, $attributes)
        {
        },
        controller: function ($scope, $element, $attrs, $timeout)
        {
            $scope.target = 0;
            $scope.start = 0;
            $scope.speed = 1;

            $scope.$watch("target", (newTarget) => {
                $scope.target = newTarget;
                $scope.tickNumber();
            });

            $scope.$watch("start", (newStart) => {
                $scope.value = newStart;
                $scope.tickNumber();
            });

            $scope.$watch("speed", (newSpeed) => {
                $scope.speed = newSpeed;
            });

            $scope.interpolate = function(current, target, delta, speed = 1.0)
            {
                if( InterpSpeed <= 0.0 )
                {
                    return target;
                }

                var distance = target - current;

                if( distance * distance < 0.000001 )
                {
                    return target;
                }

                var move = distance * Math.min(Math.max(delta * speed, 0.0), 1.0);

                return current + move;
            }
            var delta = 1 / 60;
            $scope.updateNumber = () => {
                $scope.value = $scope.interpolate($scope.value, $scope.target, 0.05, $scope.speed);
            };

            $scope.tickNumber = () => {
                if(Math.abs($scope.value - $scope.target) > 0)
                {
                    $scope.updateNumber();
                    $timeout($scope.tickNumber, 50);
                }
            };

        },
    };

});
Discussion courtesy of: Azarus

This recipe can be found in it's original form on Stack Over Flow.