Reputation: 157
I have multiple titles that are longer than 40 characters. I would like to shorten it to 40 characters and add ... if there are more characters.
I want to do something like this but in angular js:
if ( title.length > 40){
title = title.substring(0, 40) + '...'
}
Upvotes: 0
Views: 585
Reputation: 12218
I'd suggest create an angular filter, something like this:
angular.module("myApp", [])
.filter('ellipsis', function () {
return function (input, chars) {
return input.length > chars ? input.substring(0, chars) + '...' : input;
};
});
function myCtrl($scope) {
$scope.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed interdum urna vitae nisl volutpat mattis.";
}
Usage example:
<div ng-controller="myCtrl">
{{ text | ellipsis:40 }}
</div>
http://jsfiddle.net/rd13/KLDEZ/4/
Upvotes: 2