Yesterday I had problems with how to make a formatted number in my fields, but now that I have reached it, I have a problem with how to perform calculations.
index.html
<div ng-controller="MyCtrl"> <input ng-model="var.value" class="integers" symbol="°" /> <br /><br /> {{var.value * 100}} </div>
app.js
var myApp = angular.module('myApp',['ui.numeric']); angular.module('ui.numeric', []).directive('integers', function() { return { require: 'ngModel', restrict: 'EACM', link: function(scope, element, attrs, modelCtrl) { scope.$watch(element, function() { var formatted_number = accounting.formatMoney(element.val(),"", 2,",",".","%s%v"); modelCtrl.$setViewValue(formatted_number); modelCtrl.$render(); }); element.bind('blur', function() { var formatted_number = accounting.formatMoney(element.val(),"", 2,",",".","%s%v"); modelCtrl.$setViewValue(formatted_number); modelCtrl.$render(); scope.$apply(); }); element.bind('focus', function() { var plainNumber = accounting.unformat(element.val()) if(plainNumber == "0") plainNumber = ""; modelCtrl.$setViewValue(plainNumber); modelCtrl.$render(); scope.$apply(); console.log("I am focused!") }); } }; }); function MyCtrl($scope) { $scope.var = {"value":1000.36}; }
It works when the field value is less than a thousand, but when I reach 1000 , the number will be formatted with a comma of 1000 , and it will become a string and can no longer do the calculations.
All I want is to format my number fields correctly (for display), but keep the true value for performing calculations without using a separate variable in each number field.
Watch the violin. Thank!
http://jsfiddle.net/aldesabido/1syh7cne/6/
updated fiddle: http://jsfiddle.net/aldesabido/1syh7cne/14/
Solution: changed modelCtrl.setViewValue (number) to modelCtrl.viewValue (number) so that the screen is not affected by the actual value (not sure about this workaround, though.).
http://jsfiddle.net/aldesabido/1syh7cne/18/
Thanks for helping the guys!