angular.module('myApp', []) .directive('myUpperCase', function() { return { require: 'ngModel', link: function(scope, element, attrs, modelCtrl) { var upperCaseFunc = function(inputValue) { if (typeof inputValue == "undefined") { return; } var upperCasedString = inputValue.toUpperCase(); if(upperCasedString !== inputValue) { modelCtrl.$setViewValue(upperCasedString); modelCtrl.$render(); } return upperCasedString; } modelCtrl.$parsers.push(upperCaseFunc); upperCaseFunc(scope[attrs.ngModel]); } }; })
<div id="myApp" ng-app="myApp"> <input type="text" ng-model="myInput" my-upper-case /> </div>