From 23d0bacddc88810bf9105911bb404298356326f0 Mon Sep 17 00:00:00 2001 From: Caitlin Potter Date: Fri, 14 Mar 2014 00:40:47 -0400 Subject: [PATCH] fix(input): format numeric string model values correctly Previously, when a numeric string is used as a model value, the number input type would not format the value. This patch corrects this behaviour by converting to a number before performing validation, if the value is a string and not NaN. Closes #6683 --- src/ng/directive/input.js | 3 +++ test/ng/directive/inputSpec.js | 48 ++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/src/ng/directive/input.js b/src/ng/directive/input.js index cb432c52a5c7..485f01802099 100644 --- a/src/ng/directive/input.js +++ b/src/ng/directive/input.js @@ -1160,6 +1160,9 @@ function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) { } ctrl.$formatters.push(function(value) { + if (isString(value) && !isNaN(value)) { + value = Number(value); + } return validate(ctrl, 'number', ctrl.$isEmpty(value) || isNumber(value), value); }); } diff --git a/test/ng/directive/inputSpec.js b/test/ng/directive/inputSpec.js index eba3028e7bce..d2d617ef94cb 100644 --- a/test/ng/directive/inputSpec.js +++ b/test/ng/directive/inputSpec.js @@ -1538,6 +1538,54 @@ describe('input', function() { }); + it('should translate numeric base-10 strings in model to numbers', function() { + compileInput(''); + + scope.$apply(function() { + scope.age = '10'; + }); + + expect(scope.age).toBe('10'); + expect(inputElm.val()).toBe('10'); + }); + + + it('should translate numeric base-16 strings in model to numbers', function() { + compileInput(''); + + scope.$apply(function() { + scope.age = '0xFf'; + }); + + expect(scope.age).toBe('0xFf'); + expect(inputElm.val()).toBe('255'); + }); + + + it('should translate scientific notation from numeric strings in model to numbers', function() { + compileInput(''); + + scope.$apply(function() { + scope.age = '0.1e6'; + }); + + expect(scope.age).toBe('0.1e6'); + expect(inputElm.val()).toBe('100000'); + }); + + + it('should remove leading zeros from numeric strings in model', function() { + compileInput(''); + + scope.$apply(function() { + scope.age = '010'; + }); + + expect(scope.age).toBe('010'); + expect(inputElm.val()).toBe('10'); + }); + + describe('min', function() { it('should validate', function() {