To capitalize the first character of a string, We can use the charAt() to separate the first character and then use the toUpperCase() function to capitalize it.
uppercase() Function in AngularJS is used to convert the string into uppercase. It can be used when user wants to show the text in uppercase instead of lowercase. Example: In this example the string is converted into uppercase. < button id = "myButton" ng-mousedown = "upper()" >Click it!
Unfortunately in JavaScript, there isn't a capitalize or title case a string. So what we can is utilize toLowerCase() to make the entire string lower-cased and then uppercase the first letter. Convert the entire string to lower case.
use this capitalize filter
var app = angular.module('app', []);
app.controller('Ctrl', function ($scope) {
$scope.msg = 'hello, world.';
});
app.filter('capitalize', function() {
return function(input) {
return (angular.isString(input) && input.length > 0) ? input.charAt(0).toUpperCase() + input.substr(1).toLowerCase() : input;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
<div ng-controller="Ctrl">
<p><b>My Text:</b> {{msg | capitalize}}</p>
</div>
</div>
I'd say don't use angular/js as you can simply use css instead:
In your css, add the class:
.capitalize {
text-transform: capitalize;
}
Then, simply wrap the expression (for ex) in your html:
<span class="capitalize">{{ uppercase_expression }}</span>
No js needed ;)
If you use Bootstrap, you can simply add the text-capitalize
helper class:
<p class="text-capitalize">CapiTaliZed text.</p>
EDIT: just in case the link dies again:
Text Transform
Transform text in components with text capitalization classes.
lowercased text.
UPPERCASED TEXT.
CapiTaliZed Text.<p class="text-lowercase">Lowercased text.</p> <p class="text-uppercase">Uppercased text.</p> <p class="text-capitalize">CapiTaliZed text.</p>
Note how text-capitalize only changes the first letter of each word, leaving the case of any other letters unaffected.
If you are using Angular 4+ then you can just use titlecase
{{toUppercase | titlecase}}
don't have to write any pipes.
a nicer way
app.filter('capitalize', function() {
return function(token) {
return token.charAt(0).toUpperCase() + token.slice(1);
}
});
I like the answer from @TrampGuy
CSS is always (well, not always) a better choice, so: text-transform: capitalize;
is certainly the way to go.
But what if your content is all uppercase to begin with? If you try text-transform: capitalize;
on content like "FOO BAR" you'll still get "FOO BAR" in your display. To get around that issue you could put the text-transform: capitalize;
in your css, but also lowercase your string, so:
<ul>
<li class="capitalize">{{ foo.awesome_property | lowercase }}</li>
</ul>
where we are using @TrampGuy's capitalize class:
.capitalize {
text-transform: capitalize;
}
So, pretending that foo.awsome_property
would normally print "FOO BAR", it will now print the desired "Foo Bar".
If you are after performance, try to avoid using AngularJS filters as they are applied twice per each expression to check for their stability.
A better way would be to use CSS ::first-letter
pseudo-element with text-transform: uppercase;
. That can't be used on inline elements such as span
, though, so the next best thing would be to use text-transform: capitalize;
on the whole block, which capitalizes every word.
Example:
var app = angular.module('app', []);
app.controller('Ctrl', function ($scope) {
$scope.msg = 'hello, world.';
});
.capitalize {
display: inline-block;
}
.capitalize::first-letter {
text-transform: uppercase;
}
.capitalize2 {
text-transform: capitalize;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
<div ng-controller="Ctrl">
<b>My text:</b> <div class="capitalize">{{msg}}</div>
<p><b>My text:</b> <span class="capitalize2">{{msg}}</span></p>
</div>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With