Created
November 28, 2016 11:50
-
-
Save MeTe-30/c1c9b14bf63ef448fca96630c2e57fad to your computer and use it in GitHub Desktop.
Money-Price formatting directive without affecting on ngModel (:Integer) | AngularJs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// My answer to question http://stackoverflow.com/questions/38062482/money-formatting-directive-in-angular/38112095#38112095 | |
app.directive('price', [function () { | |
return { | |
require: 'ngModel', | |
link: function (scope, element, attrs, ngModel) { | |
attrs.$set('ngTrim', "false"); | |
var formatter = function(str, isNum) { | |
str = String( Number(str || 0) / (isNum?1:100) ); | |
str = (str=='0'?'0.0':str).split('.'); | |
str[1] = str[1] || '0'; | |
return str[0].replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1,') + '.' + (str[1].length==1?str[1]+'0':str[1]); | |
} | |
var updateView = function(val) { | |
scope.$applyAsync(function () { | |
ngModel.$setViewValue(val || ''); | |
ngModel.$render(); | |
}); | |
} | |
var parseNumber = function(val) { | |
var modelString = formatter(ngModel.$modelValue, true); | |
var sign = { | |
pos: /[+]/.test(val), | |
neg: /[-]/.test(val) | |
} | |
sign.has = sign.pos || sign.neg; | |
sign.both = sign.pos && sign.neg; | |
if (!val || sign.has && val.length==1 || ngModel.$modelValue && Number(val)===0) { | |
var newVal = (!val || ngModel.$modelValue && Number()===0?'':val); | |
if (ngModel.$modelValue !== newVal) | |
updateView(newVal); | |
return ''; | |
} | |
else { | |
var valString = String(val || ''); | |
var newSign = (sign.both && ngModel.$modelValue>=0 || !sign.both && sign.neg?'-':''); | |
var newVal = valString.replace(/[^0-9]/g,''); | |
var viewVal = newSign + formatter(angular.copy(newVal)); | |
if (modelString !== valString) | |
updateView(viewVal); | |
return (Number(newSign + newVal) / 100) || 0; | |
} | |
} | |
var formatNumber = function(val) { | |
if (val) { | |
var str = String(val).split('.'); | |
str[1] = str[1] || '0'; | |
val = str[0] + '.' + (str[1].length==1?str[1]+'0':str[1]); | |
} | |
return parseNumber(val); | |
} | |
ngModel.$parsers.push(parseNumber); | |
ngModel.$formatters.push(formatNumber); | |
} | |
}; | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment