Adds watcher for changes to value or currency scope variables

This commit is contained in:
Sam Cheng Hung 2018-07-31 16:23:45 +08:00
commit 1895e0dbeb

View file

@ -48,27 +48,28 @@ angular.module('bitcoincom.directives')
return '2'; return '2';
}; };
switch (getDecimalPlaces($scope.currency)) { var formatNumbers = function(currency, value) {
switch (getDecimalPlaces(currency)) {
case '0': case '0':
var valueFormatted = numberWithCommas(Math.round(parseFloat($scope.value))); var valueFormatted = numberWithCommas(Math.round(parseFloat(value)));
buildAmount(valueFormatted, '', ''); buildAmount(valueFormatted, '', '');
break; break;
case '2': case '2':
var valueProcessing = parseFloat(parseFloat($scope.value).toFixed(2)); var valueProcessing = parseFloat(parseFloat(value).toFixed(2));
var valueFormatted = numberWithCommas(valueProcessing); var valueFormatted = numberWithCommas(valueProcessing);
buildAmount(valueFormatted, '', ''); buildAmount(valueFormatted, '', '');
break; break;
case '3': case '3':
var valueProcessing = parseFloat(parseFloat($scope.value).toFixed(3)); var valueProcessing = parseFloat(parseFloat(value).toFixed(3));
var valueFormatted = numberWithCommas(valueProcessing); var valueFormatted = numberWithCommas(valueProcessing);
buildAmount(valueFormatted, '', ''); buildAmount(valueFormatted, '', '');
break; break;
case '8': case '8':
var valueFormatted = parseFloat($scope.value).toFixed(8); var valueFormatted = parseFloat(value).toFixed(8);
if (parseFloat($scope.value) == 0) { if (parseFloat(value) == 0) {
buildAmount('0', '', ''); buildAmount('0', '', '');
} else { } else {
buildAmount(valueFormatted, '', ''); buildAmount(valueFormatted, '', '');
@ -79,6 +80,12 @@ angular.module('bitcoincom.directives')
} }
break; break;
} }
}
formatNumbers($scope.currency, $scope.value);
$scope.$watchGroup(['currency', 'value'], function() {
formatNumbers($scope.currency, $scope.value);
});
}] }]
}; };
} }