toFixed(2); in angular
HTML
<body ng-app="app" ng-controller="ctrl">
<input my-directive name="test" ng-model="item" placeholder="Type in some lowercase text..." style="display:block;width:100%;">
<button type="button" ng-click="item = 1.3434">Set model to "UPPERCASE_TEXT"</button>
<label>Model: {{item}}</label>
</body>
JavaScript
angular.module("app", []);
angular.module("app").controller("ctrl", function($scope){
$scope.item = '';
$scope.$watch('item', function(newVal, oldVal){
if (newVal !== oldVal){
console.log(newVal);
}
});
});
angular.module("app").directive("myDirective", function(){
return {
require: 'ngModel',
link: function(scope, element, attrs, ngModelController) {
ngModelController.$parsers.push(function(data) {
//convert data from view format to model format
return data.toFixed(2); //converted
});
ngModelController.$formatters.push(function(data) {
//convert data from model format to view format
return data.toFixed(2); //converted
});
}
};
});