bug(money-mask): wrong number formating when value and decimals are setted asychronously
Hi there.
I'm facing a problem with the money-mask. When the value and decimals are coming from a response from the server the money-mask formats the value wrong.
Codepen: http://codepen.io/marcomafessolli/pen/akEEYR
Looking into the code, it happens here: clearDelimitersAndLeadingZeros((parseFloat(value)).toFixed(decimals));, specifically here: (parseFloat(value)).toFixed(decimals). Because of that, a number with 3 decimals, when first setted, goes into the parser formatter with 2 decimals and gets rounded, losing one of the decimals.
The problem seems to be the order of how the values are applied. The decimal value need to be applied first or you will loose precision.
Change your code to:
$timeout(function () {
$scope.decimals2 = 3;
$timeout(function () {
$scope.moneyDecimals2 = 4.835;
});
});
and I think your problem will be resolved.
Hmmm, I see. I think there is no other way, yeah? In my scenario, decimals and moneyDecimals are coming from a http request, take a look;
Controller:
$http.get(/products/1)
.then(function (response) {
$scope.model = response.data
});
View:
<input ng-model="model.moneyDecimal" ui-money-mask="model.decimals"/>
In this case there's nothing to do, correct?!
Unfortunately yes, because the decimals setter is async.