From 773436bd02d351297af034af3f8f51f208eb642c Mon Sep 17 00:00:00 2001 From: Jose Pinto Date: Wed, 9 Jan 2019 18:09:36 -0300 Subject: [PATCH] nuevas directivas --- src/js/stringToNumber-directive.js | 14 ++++++++++++++ src/js/uppercaseOnly-directive.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 src/js/stringToNumber-directive.js create mode 100644 src/js/uppercaseOnly-directive.js diff --git a/src/js/stringToNumber-directive.js b/src/js/stringToNumber-directive.js new file mode 100644 index 0000000..eaf86f2 --- /dev/null +++ b/src/js/stringToNumber-directive.js @@ -0,0 +1,14 @@ +angular.module('focaDirectivas') + .directive('stringToNumber', function () { + return { + require: 'ngModel', + link: function (scope, element, attrs, ngModel) { + ngModel.$parsers.push(function (value) { + return '' + value; + }); + ngModel.$formatters.push(function (value) { + return parseFloat(value); + }); + } + }; + }); \ No newline at end of file diff --git a/src/js/uppercaseOnly-directive.js b/src/js/uppercaseOnly-directive.js new file mode 100644 index 0000000..adb632d --- /dev/null +++ b/src/js/uppercaseOnly-directive.js @@ -0,0 +1,33 @@ +angular.module('focaDirectivas') + .directive('uppercaseOnly', [ + function() { + return { + restrict: 'A', + 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)) { + return value; + } + return value.toUpperCase(); + } + + ctrl.$formatters.push(formatter); + ctrl.$parsers.push(parser); + } + }; + } + ]); \ No newline at end of file -- 1.9.1