Last active
August 29, 2015 14:22
-
-
Save benjamincharity/94b4a3f2377e4587ebb6 to your computer and use it in GitHub Desktop.
Allow users to input dollars while keeping your model dealing with cents.
Credit: http://stackoverflow.com/a/18743619/722367 & http://stackoverflow.com/questions/6095795/convert-a-javascript-string-variable-to-decimal-money
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
angular.module('admin') | |
.directive('dollarsToCents', function(){ | |
return { | |
require: 'ngModel', | |
link: function(scope, element, attrs, ngModel) { | |
if(ngModel) { // Don't do anything unless we have a model | |
ngModel.$parsers.push(function(value) { | |
return value*100; | |
}); | |
ngModel.$formatters.push(function(value) { | |
return parseFloat(value/100).toFixed(2); | |
}); | |
} | |
} | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment