I\'ve tried using the uppercase filter but it does not work. I\'ve tried doing it two ways:
<
The accepted answer causes problems if someone tries to enter a lowercase letter at the beginning of an existing string.. The cursor moves to the end of the string after each key press. Here's a simple solution that addresses all the issues:
directive('uppercased', function() {
return {
require: 'ngModel',
link: function(scope, element, attrs, modelCtrl) {
modelCtrl.$parsers.push(function(input) {
return input ? input.toUpperCase() : "";
});
element.css("text-transform","uppercase");
}
};
})
Here's a fiddle: http://jsfiddle.net/36qp9ekL/1710/
Please see the other answer below, which is superior to this one.
this answer is based on the answer here: How to autocapitalize the first character in an input field in AngularJS?.
I'd imagine that what you'd want would be a parser function like this:
angular
.module('myApp', [])
.directive('capitalize', function() {
return {
require: 'ngModel',
link: function(scope, element, attrs, modelCtrl) {
var capitalize = function(inputValue) {
if (inputValue == undefined) inputValue = '';
var capitalized = inputValue.toUpperCase();
if (capitalized !== inputValue) {
// see where the cursor is before the update so that we can set it back
var selection = element[0].selectionStart;
modelCtrl.$setViewValue(capitalized);
modelCtrl.$render();
// set back the cursor after rendering
element[0].selectionStart = selection;
element[0].selectionEnd = selection;
}
return capitalized;
}
modelCtrl.$parsers.push(capitalize);
capitalize(scope[attrs.ngModel]); // capitalize initial value
}
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
<input type="text" ng-model="name" capitalize>
</div>
I would just use the filter itself in the controller:
$filter('uppercase')(this.yourProperty)
just keep in mind that, if you you are going to use it inside a controller, for example, you need to inject this filter:
app.controller('FooController', ['$filter', function($filter) ...
When used with Bootstrap, just add text-uppercase
to input's class attribute.
Solution with cursor shift fix
.directive('titleCase', function () {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, element, attrs, modelCtrl) {
var titleCase = function (input) {
let first = element[0].selectionStart;
let last = element[0].selectionEnd;
input = input || '';
let retInput = input.replace(/\w\S*/g, function (txt) { return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); });
if (input !== retInput) {
modelCtrl.$setViewValue(retInput);
attrs.ngModel = retInput;
modelCtrl.$render();
if (!scope.$$phase) {
scope.$apply(); // launch digest;
}
}
element[0].selectionStart = first;
element[0].selectionEnd = last;
return retInput;
};
modelCtrl.$parsers.push(titleCase);
titleCase(scope[attrs.ngModel]); // Title case initial value
}
};
});
You cannot make filter on ng-model since it has to be assignable. the workaround is either parser, or simply ng-change.
<input ng-model="some" ng-change="some = (some | uppercase)" />
This should work.