admin管理员组

文章数量:1294661

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: /

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

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/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

Share Improve this question edited May 23, 2017 at 12:13 CommunityBot 11 silver badge asked Jan 21, 2014 at 8:36 Liad LivnatLiad Livnat 7,47517 gold badges61 silver badges98 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

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', ['$pile',function($pile,$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($pile('<span>{{millis}}</span>')($scope));
            } else {
                $element.append($pile($element.contents())($scope));
            }

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

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

        },
    };

});

本文标签: javascriptAngularJS counter to count up to a target numberStack Overflow