uppercaseOnly-directive.js 1.35 KB
angular.module('focaDirectivas')
    .directive('input', [
        function() {
            return {
                restrict: 'E',
                require: '?ngModel',
                link: function(scope, element, attrs, ctrl) {

                    function parser(value) {
                        if (ctrl.$isEmpty(value)) {
                            return value;
                        }
                        var formatedValue = value.toUpperCase();
                        if (ctrl.$viewValue !== formatedValue) {
                            ctrl.$setViewValue(formatedValue);
                            ctrl.$render();
                        }
                        return formatedValue;
                    }

                    function formatter(value) {
                        if (ctrl.$isEmpty(value) || typeof value !== 'string') {
                            return value;
                        }
                        return value.toUpperCase();
                    }
                    if(attrs.type === 'text' &&
                        !attrs.readonly &&
                        !attrs.disabled &&
                        !attrs.uibDatepickerPopup) {
    
                        ctrl.$formatters.push(formatter);
                        ctrl.$parsers.push(parser);
                    }
                }
            };
        }
    ]);