diff --git a/angular-countUp.js b/angular-countUp.js index f0e55bb..28869da 100644 --- a/angular-countUp.js +++ b/angular-countUp.js @@ -7,7 +7,7 @@ // * **Author:** Jamie Perkins // // Creates a counting animation for numbers - // REQUIRED attributes: + // REQUIRED attributes: // - endVal // // DEPENDENCY: countUp.js @@ -18,7 +18,7 @@ /** * count-up attribute directive - * + * * @param {number} startVal - (optional) The value you want to begin at, default 0 * @param {number} countUp - The value you want to arrive at * @param {number} duration - (optional) Duration in seconds, default 2. @@ -75,7 +75,7 @@ dur = Number(dur) || 2; dec = Number(dec) || 0; - // construct countUp + // construct countUp var countUp = new CountUp($el[0], sta, end, dec, dur, options); if (end > 999) { // make easing smoother for large numbers @@ -118,17 +118,25 @@ }); } - $scope.$watch('endVal', function (newValue, oldValue) { + function onWatch(newValue, oldValue, forceReload){ if (newValue === null || newValue === oldValue) { return; } - if (countUp !== null) { + if (countUp !== null && !forceReload) { countUp.update($scope.endVal); } else { countUp = createCountUp($scope.startVal, $scope.endVal, $scope.decimals, $scope.duration); animate(); } + } + + $scope.$watch('decimals', function(newValue, oldValue) { + onWatch(newValue, oldValue, true); + }); + + $scope.$watch('endVal', function (newValue, oldValue) { + onWatch(newValue, oldValue, false); }); } }; diff --git a/dist/angular-countUp.min.js b/dist/angular-countUp.min.js index 8b02a25..25c0e81 100644 --- a/dist/angular-countUp.min.js +++ b/dist/angular-countUp.min.js @@ -1 +1 @@ -!function(n){"use strict";var t=n.module("countUpModule",[]);t.directive("countUp",["$filter",function(t){return{restrict:"A",scope:{startVal:"=?",endVal:"=?",duration:"=?",decimals:"=?",reanimateOnClick:"=?",filter:"@",options:"=?"},link:function(a,e,i){function r(){var n=a.filter.split(":"),e=n.shift();return function(a){var i=[a];return Array.prototype.push.apply(i,n),a=t(e).apply(null,i)}}function l(n,t,a,i){n=n||0,isNaN(n)&&(n=Number(n.match(/[\d\-\.]+/g).join(""))),t=t||0,isNaN(t)&&(t=Number(t.match(/[\d\-\.]+/g).join(""))),i=Number(i)||2,a=Number(a)||0;var r=new CountUp(e[0],n,t,a,i,u);return t>999&&(r=new CountUp(e[0],n,t-100,a,i/2,u)),r}function o(){s.reset(),a.endVal>999?s.start(function(){s.update(a.endVal)}):s.start()}var u={};if(a.filter){var c=r();u.formattingFn=c}a.options&&n.extend(u,a.options);var s=l(a.startVal,a.endVal,a.decimals,a.duration);i.scrollSpyEvent?a.$on(i.scrollSpyEvent,function(n,t){t===i.id&&o()}):o();var d=!n.isDefined(a.reanimateOnClick)||a.reanimateOnClick;d&&e.on("click",function(){o()}),a.$watch("endVal",function(n,t){null!==n&&n!==t&&(null!==s?s.update(a.endVal):(s=l(a.startVal,a.endVal,a.decimals,a.duration),o()))})}}}])}(angular); \ No newline at end of file +!function(n){"use strict";var t=n.module("countUpModule",[]);t.directive("countUp",["$filter",function(t){return{restrict:"A",scope:{startVal:"=?",endVal:"=?",duration:"=?",decimals:"=?",reanimateOnClick:"=?",filter:"@",options:"=?"},link:function(a,i,e){function r(){var n=a.filter.split(":"),i=n.shift();return function(a){var e=[a];return Array.prototype.push.apply(e,n),a=t(i).apply(null,e)}}function l(n,t,a,e){n=n||0,isNaN(n)&&(n=Number(n.match(/[\d\-\.]+/g).join(""))),t=t||0,isNaN(t)&&(t=Number(t.match(/[\d\-\.]+/g).join(""))),e=Number(e)||2,a=Number(a)||0;var r=new CountUp(i[0],n,t,a,e,c);return t>999&&(r=new CountUp(i[0],n,t-100,a,e/2,c)),r}function o(){d.reset(),a.endVal>999?d.start(function(){d.update(a.endVal)}):d.start()}function u(n,t,i){null!==n&&n!==t&&(null===d||i?(d=l(a.startVal,a.endVal,a.decimals,a.duration),o()):d.update(a.endVal))}var c={};if(a.filter){var s=r();c.formattingFn=s}a.options&&n.extend(c,a.options);var d=l(a.startVal,a.endVal,a.decimals,a.duration);e.scrollSpyEvent?a.$on(e.scrollSpyEvent,function(n,t){t===e.id&&o()}):o();var f=!n.isDefined(a.reanimateOnClick)||a.reanimateOnClick;f&&i.on("click",function(){o()}),a.$watch("decimals",function(n,t){u(n,t,!0)}),a.$watch("endVal",function(n,t){u(n,t,!1)})}}}])}(angular); \ No newline at end of file