AngularJS counter to count up to a target number

爱⌒轻易说出口 提交于 2019-12-04 16:04:23

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

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();
        }]
    }}])

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.

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

        },
    };

});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!