AngularJS – How to Limit String Length using Filter?
In this post we will give you information about AngularJS – How to Limit String Length using Filter?. Hear we will give you detail about AngularJS – How to Limit String Length using Filter?And how to use it also give you demo for it if it is necessary.
Some case we have enough space to display content, for example you have articale blog and you want to display saveral articale on homepage with limited content like this way :
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate....
You can do it easily if you are work on any framework or something, but if you want to do on angular then you need to create your own filter for this. you can set limit of characters using following filter:
Create Filter:
var app = angular.module('myApp', []);
app.filter('limitChar', function () {
return function (content, length, tail) {
if (isNaN(length))
length = 50;
if (tail === undefined)
tail = "...";
if (content.length
return content;
}
else {
return String(content).substring(0, length-tail.length) + tail;
}
};
});
Use Filter:
If you don’t want to set limit then it will take 50 auto.
{{ randomText | limitChar }}
Bellow example you can see, we can set limit specific as we want.
{{ randomText | limitChar:10 }}
Bellow example you can see, we can set limit specific as well as we can specify last char as we want.
{{ randomText | limitChar:10:"...!!!" }}
Hope this code and post will helped you for implement AngularJS – How to Limit String Length using Filter?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs