Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Angular Count-To

This project is an animated counter for Angularjs. The directive counts from one number to another over a configured duration.
[Demo](http://sparkalow.github.io/angular-count-to/)
[Demo](http://webkoils.github.io/angular-count-to/)

## How to use angular count-to

Expand All @@ -22,7 +22,10 @@ Apply the directive to a dom element.
```
<span count-to="{{countTo}}" value="{{countFrom}}" duration="4"></span>
```

Optionally you can use a filter on the value
```
<span count-to="{{countTo}}" value="{{countFrom}}" duration="4" filter="number:2"></span>
```

### Attributes

Expand All @@ -31,4 +34,5 @@ The following attributes can be set as numbers on the directive element.
- ```count-to``` the number to count to.
- ```value``` the number to start counting from.
- ```duration``` how long the count should take in seconds.
- ```filter``` a filter to apply to the displayed number

4 changes: 2 additions & 2 deletions build/angular-count-to.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions src/count-to.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ var countTo = angular.module('countTo', [])
replace: false,
scope: true,
link: function (scope, element, attrs) {

var e = element[0];
var num, refreshInterval, duration, steps, step, countTo, value, increment;

var calculate = function () {
refreshInterval = 30;
step = 0;
Expand All @@ -28,9 +26,10 @@ var countTo = angular.module('countTo', [])
if (step >= steps) {
$timeout.cancel(scope.timoutId);
num = countTo;
e.textContent = countTo;
e.textContent = (attrs.filter ? scope.$eval("countTo |"+attrs.filter+"",{countTo:countTo}) : countTo);
} else {
e.textContent = Math.round(num);
e.textContent = (attrs.filter ? scope.$eval("num |"+attrs.filter+"",{num:num}) : Math.round(num));

tick();
}
}, refreshInterval);
Expand Down
26 changes: 26 additions & 0 deletions test/unit/directivesSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,31 @@ describe('count-to directive', function () {
}, 0)

}));

it('should display unformatted number when no filter is set', inject(function ($compile, $rootScope, $timeout ) {
element = angular.element('<span count-to="10" duration="5" value="200" filter=""></span>');
scope = $rootScope.$new();
$compile(element)(scope);
$rootScope.$digest();

$timeout.flush();
$timeout(function(){
expect(element.text()).toBe('200');
}, 0)

}));

it('should display formatted number', inject(function ($compile, $rootScope, $timeout ) {
element = angular.element('<span count-to="10" duration="5" value="200" filter="number:2"></span>');
scope = $rootScope.$new();
$compile(element)(scope);
$rootScope.$digest();

$timeout.flush();
$timeout(function(){
expect(element.text()).toBe('200.00');
}, 0)

}));

});