benjamincharity
6/10/2015 - 5:27 PM

Allow users to input dollars while keeping your model dealing with cents. Credit: http://stackoverflow.com/a/18743619/722367 & http://stacko

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);
                });

            }
        }
    };
});