I've tried using the uppercase filter but it does not work. I've tried doing it two ways:
<input type="text" ng-model="test" uppercase/>
and
<input type="text" ng-model="{{test | uppercase}}"/>
The 2nd triggers a javascript error:
Syntax Error: Token 'test' is unexpected, expecting [:]
I want the text to be forced to uppercase as the user types in the textbox.
How can I do that?
this answer is based on the answer here: Angular.js: How to autocapitalize an input field?.
I'd imagine that what you'd want would be a parser function like this:
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) {
modelCtrl.$setViewValue(capitalized);
modelCtrl.$render();
}
return capitalized;
}
modelCtrl.$parsers.push(capitalize);
capitalize(scope[attrs.ngModel]); // capitalize initial value
}
};
});
...and HTML like this:
<input type="text" ng-model="name" capitalize>
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/23/
The idea is to show (not transform) the string as uppercase at client side and transform into uppercase at server side (users can always control what happens at client side). So:
1) in the html:
<input id="test" type="text" ng-model="test">
here no uppercase transformation.
2) in the css:
#test {text-transform: uppercase;}
data is shown as uppercase, but actually still lowercase, if user typed in lowercase. 3) turn the string into uppercase at server side when inserting into database.
= = = = = for playing around, can try follow:
<input type="text" ng-model="test" ng-change="test=test.toUpperCase();">
<input type="text" ng-model="test" ng-blur="test=test.toUpperCase();">
but I think ng-change or ng-blur ways are not necessary for your case.
When used with Bootstrap, just add text-uppercase
to input's class attribute.
This will not work at all.
ng-model
is for specifying which field / property from the scope should be bound to the model. Also, ng-model
does not accept an expression as value. Expressions in angular.js are things between {{
and }}
.
The uppercase
filter could used in the output and everywhere where expressions are allowed.
You cannot do what you want to do, but you could use CSS's text-transform
to at least display everything in uppercase.
If you want to have the value of a text field in uppercase letters you can achieve this with some custom JavaScript.
In your controller:
$scope.$watch('test', function(newValue, oldValue) {
$scope.$apply(function() {
$scope.test = newValue.toUpperCase();
}
});
Don't forget to include 'ngSanitize' in your module!
app.directive('capitalize', function() {
return {
restrict: 'A', // only activate on element attribute
require: '?ngModel',
link : function(scope, element, attrs, modelCtrl) {
var capitalize = function(inputValue) {
if(inputValue) {
var capitalized = inputValue.toUpperCase();
if (capitalized !== inputValue) {
modelCtrl.$setViewValue(capitalized);
modelCtrl.$render();
}
return capitalized;
}
};
modelCtrl.$parsers.push(capitalize);
capitalize(scope[attrs.ngModel]); // capitalize initial value
}
};
});
Pay attention to "?" in "require: '?ngModel',"... only then worked my application.
"if(inputValue) {...}" For no undefined error occurs
one of the simple way is,
<input type="text" ng-model="test" ng-change="upper(test)/>
just do below 2 line code in your js file,
$scope.upper = function(test){
$scope.test = test.toUpperCase();
}
here is my fiddle http://jsfiddle.net/mzmmohideen/36qp9ekL/299/
it is just an alternative , you can use this " text- transform : capitalize ; " in your css and the text entry will be capitalized. unless the user types it in capital letters everywhere.
it is just an alternative ^^