diff --git a/src/rating/rating.js b/src/rating/rating.js index 55ed9e0d44..673c3b2a99 100644 --- a/src/rating/rating.js +++ b/src/rating/rating.js @@ -12,6 +12,13 @@ angular.module('ui.bootstrap.rating', []) this.init = function(ngModelCtrl_) { ngModelCtrl = ngModelCtrl_; ngModelCtrl.$render = this.render; + + ngModelCtrl.$formatters.push(function(value) { + if (angular.isNumber(value) && value << 0 !== value) { + value = Math.round(value); + } + return value; + }); this.stateOn = angular.isDefined($attrs.stateOn) ? $scope.$parent.$eval($attrs.stateOn) : ratingConfig.stateOn; this.stateOff = angular.isDefined($attrs.stateOff) ? $scope.$parent.$eval($attrs.stateOff) : ratingConfig.stateOff; @@ -58,6 +65,7 @@ angular.module('ui.bootstrap.rating', []) this.render = function() { $scope.value = ngModelCtrl.$viewValue; }; + }]) .directive('rating', function() { diff --git a/src/rating/test/rating.spec.js b/src/rating/test/rating.spec.js index c841783944..cd92771ac9 100644 --- a/src/rating/test/rating.spec.js +++ b/src/rating/test/rating.spec.js @@ -73,6 +73,20 @@ describe('rating directive', function () { expect($rootScope.rate).toBe(3); }); + it('rounds off the number of stars shown with decimal values', function() { + $rootScope.rate = 2.1; + $rootScope.$digest(); + + expect(getState()).toEqual([true, true, false, false, false]); + expect(element.attr('aria-valuenow')).toBe('2'); + + $rootScope.rate = 2.5; + $rootScope.$digest(); + + expect(getState()).toEqual([true, true, true, false, false]); + expect(element.attr('aria-valuenow')).toBe('3'); + }); + it('changes the number of selected icons when value changes', function() { $rootScope.rate = 2; $rootScope.$digest();