Skip to content

Commit

Permalink
fix(ui-number-mask): show "0" if model is 0
Browse files Browse the repository at this point in the history
Show 0, instead of undefined, if the model value has changed to 0.
When ui-number-mask="0" is used and the model changes the value
to 0, then undefined is displayed in the input field.
The cause of the error is, that clearDelimitersAndLeadingZero
does not only clear leading zeros, but also a single 0
(what is equal to the vaue 0). -> Do not strip a single 0.

Closes ##111
  • Loading branch information
Micha Reiser committed Nov 19, 2015
1 parent a93cf5a commit 1bae78c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/global/number/number.test.js
Expand Up @@ -111,6 +111,16 @@ describe('ui-number-mask', function() {
});
}));

it('should show zero when the model value is zero and the precision is set to 0', inject(function($rootScope) {
var input = TestUtil.compile('<input ng-model="model" ui-number-mask="0">');
var model = input.controller('ngModel');


$rootScope.model = 0;
$rootScope.$digest();
expect(model.$viewValue).toBe('0');
}));

it('should accept negative numbers if "ui-negative-number" is defined', function() {
var input = TestUtil.compile('<input ng-model="model" ui-number-mask ui-negative-number>');
var model = input.controller('ngModel');
Expand Down
7 changes: 5 additions & 2 deletions src/helpers.js
Expand Up @@ -6,9 +6,12 @@ module.exports = m.name;

m.factory('PreFormatters', [function(){
function clearDelimitersAndLeadingZeros(value) {
if (value === '0') {
return '0';
}

var cleanValue = value.replace(/^-/,'').replace(/^0*/, '');
cleanValue = cleanValue.replace(/[^0-9]/g, '');
return cleanValue;
return cleanValue.replace(/[^0-9]/g, '');
}

function prepareNumberToFormatter (value, decimals) {
Expand Down

0 comments on commit 1bae78c

Please sign in to comment.