I'm trying to get and update the data (float) with two decimal places, for example. I have a text-box which allows user to input float number, I use one directive to convert into 2 decimal places:
<input type="text" ng-model="data" name="data" smart-float />
app.directive('smartFloat', function() {
return {
require: 'ngModel',
link: function(scope, elm, attrs, ctrl) {
ctrl.$parsers.unshift(function(viewValue) {
var FLOAT_REGEXP = /^\-?\d+(\.\d+)?$/;
if (FLOAT_REGEXP.test(viewValue)) {
ctrl.$setValidity('float', true);
return parseFloat(Math.round(viewValue * 100) / 100).toFixed(2);
} else {
ctrl.$setValidity('float', false);
return undefined;
}
});
}
};
});
This directive works okay get 2 decimal places on the model, the thing is I want to update back into the text-box, when user click on check
button, example:
if user input 1.345
on the text-box, then click check button, the value 1.35
should update back to the text-box:
Is there any way around?
I tried to trigger $digest
loop in checkFn
using $scope.$digest()
but it does not work out.
The flunker